在宏上工作,该宏将转到第一张工作表。我正在使用:
Sub GoToFirstSheet()
On Error Resume Next
Sheets(1).Select
End Sub
Run Code Online (Sandbox Code Playgroud)
但是,如果工作表1是隐藏的,则将无法工作。如何合并进入未隐藏的第一张纸的方法?
Sid*_*out 10
像这样吗
Option Explicit
Sub GoToFirstSheet()
Dim i As Long
For i = 1 To ThisWorkbook.Sheets.Count
On Error Resume Next
Sheets(i).Activate
If Err.Number = 0 Then Exit For
Next i
End Sub
Run Code Online (Sandbox Code Playgroud)
应该这样做:
Option Explicit
Sub GoToFirstSheet()
Dim ws As Worksheet 'declare a worksheet variable
'loop through all the worksheets in the workbook
For Each ws In ThisWorkbook.Worksheets
'If the sheet is not hidden
If ws.Visible = xlSheetVisible Then
ws.Select 'select it
Exit For 'exit the loop
End If
Next ws
End Sub
Run Code Online (Sandbox Code Playgroud)