Function DeleteRows()
Dim wb As Workbook
Set wb = Workbooks(ThisWorkbook.Name)
Debug.Print wb.Name
Dim wsName As String
Dim ws As Worksheet
wsName = "SheetX"
Set ws = wb.Worksheets(wsName)
Debug.Print ws.Name
With ws
Debug.Print "ws.name = " & ws.Name
Rows("3:31").Select
Selection.Delete Shift:=xlUp
End With
End Function
Run Code Online (Sandbox Code Playgroud)
我有这个函数 DeleteRows,它从工作表“SheetX”中删除 3:31 的行。
这仅在“SheetX”是显示在屏幕上的工作表时有效。
I want it to work when another sheet, lets say "SheetY", in the workbook is displayed. How can I do this?
One way would be to: 1-activate "SheetY" 2-delete the rows 3-activate "SheetX"
But this would not be transparent to the user. Appreciate the help.
您应该始终尽量避免使用 Select 语句。它们几乎总是不必要的。该函数应该是一个 Sub(因为它没有输出)并且可以重写为一个单行:
Sub DeleteRows()
ThisWorkbook.Sheets("SheetX").Range("3:31").Delete xlUp
End Sub
Run Code Online (Sandbox Code Playgroud)