如果另一个未被隐藏,则隐藏工作表

时间:2020-04-08 00:57:55

标签: excel vba

我不确定在这里是否遗漏了任何东西。但是我想做的是,如果未隐藏任何包含 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

1 个答案:

答案 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