我不确定在这里是否遗漏了任何东西。但是我想做的是,如果未隐藏任何包含 Hello 的工作表名称,则隐藏Sheet 1
;反之亦然。知道我的代码出了什么问题吗?谢谢!
Private Sub HideIrrelevantSheets()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If ws.Name Like "Hello*" Then
Worksheets("Sheet 1").Visible = xlSheetHidden
Else
Worksheets("Sheet 1").Visible = xlSheetVisible
End If
Next ws
End Sub
答案 0 :(得分:1)
我要尝试的是隐藏未隐藏任何包含Hello的工作表名称的工作表1;反之亦然。
然后,除了测试名称之外,还需要测试工作表是否可见:
Private Sub HideIrrelevantSheets()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
If ws.Name Like "Hello*" Then
If ws.Visible = xlSheetVisible Then
Thisworkbook.Worksheets("Sheet 1").Visible = xlSheetHidden
Else
Thisworkbook.Worksheets("Sheet 1").Visible = xlSheetVisible
End If
Exit Sub ' no need to keep looping, or maybe use Exit For
End If
Next ws
End Sub