可见所有工作表,除了指定的两个工作表

Visible all sheets except specified two sheets

我已经编写了一些代码来通过更改第一个工作表中的值来隐藏和取消隐藏工作表,如何才能使除前 2 个工作表之外的所有工作表都可见?

Private Sub Worksheet_Change(ByVal Target As Range)
    If Range("H5").Value = "ADMIN" Then
        Sheets(2).Visible = True
        Sheets(2).Activate
    Else
        Sheets(2).Visible = xlVeryHidden
    End If
    If Range("G8").Value = True And Range("H5").Value = "" Then
        Sheets(3).Visible = True 'I want to visible all sheets except first two sheets.
        Sheets(4).Visible = True
        Sheets(1).Visible = xlVeryHidden
        Sheets(2).Visible = xlVeryHidden
    Else
        Sheets(3).Visible = xlVeryHidden
        Sheets(4).Visible = xlVeryHidden
    End If
End Sub

您需要做的是遍历工作簿中的所有 Sheets,如果您的 Sheet.Index 大于 2,则使 sheet 可见。

参见下面的循环:

Dim Sht As Worksheet

' loop through all worksheets in this workbook
For Each Sht In ThisWorkbook.Worksheets
    If Sht.Index > 2 Then ' check if index > 2
        Sht.Visible = xlSheetVisible
    Else
        Sht.Visible = xlVeryHidden
    End If
Next Sht

完整代码

Private Sub Worksheet_Change(ByVal Target As Range)

Dim Sht As Worksheet

If Range("H5").Value = "ADMIN" Then
    Sheets(2).Visible = True
    Sheets(2).Activate
Else
    Sheets(2).Visible = xlVeryHidden
End If

If Range("G8").Value = True And Range("H5").Value = "" Then
    For Each Sht In ThisWorkbook.Worksheets
        If Sht.Index > 2 Then
            Sht.Visible = xlSheetVisible

        End If
    Next Sht

End If

End Sub

编辑以反映 Adnan 最后的代码

您可以试试这个代码:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim iSht As Long

    Sheets(2).Visible = xlVeryHidden '<--| you'll make it visible if the case (see below)
    Select Case Range("H5").Value '<--| check for H5 cell only once
        Case "ADMIN"
            Sheets(2).Visible = True
            Sheets(2).Activate
        Case ""
            If Range("G8").Value Then '<--| check for it only when H5 cell value <> "ADMIN"
                For iSht = 3 To Sheets.count '<--| loop through sheets indexes greater than 2, and avoid 'If ... Then' check
                    Sheets(iSht).Visible = True
                Next iSht
                Sheets(1).Visible = xlVeryHidden '<--| hide first sheet only once
            End If
    End Select
End Sub

它做的事情和你的一样,但有一些逻辑上的改进:

  • 不检查两次 Range("H5").Value

  • Range("H5").Value 是 "ADMIN"

    之后,
  • 不会无用地检查 Range("G8").Value

  • 不会在每个 For Each Sht In ThisWorkbook.Worksheets 循环中进行 If Sht.Index > 2 Then 检查

  • 不在每个 For Each Sht In ThisWorkbook.Worksheets 循环中设置 Sheets(1).Visible = xlVeryHidden

Private Sub Worksheet_Change(ByVal Target As Range)

Dim Sht As Worksheet

If Range("H5").Value = "ADMIN" Then
    Sheets(2).Visible = True
    Sheets(2).Activate
Else
    Sheets(2).Visible = xlVeryHidden
End If

If Range("G8").Value = True And Range("H5").Value = "" Then
    For Each Sht In ThisWorkbook.Worksheets
        If Sht.Index > 2 Then
            Sht.Visible = xlSheetVisible
            Sheets(1).Visible = xlVeryHidden
'     Else
'         Sht.Visible = xlVeryHidden
        End If
    Next Sht
'Else
'    Sheets(3).Visible = xlVeryHidden
'    Sheets(4).Visible = xlVeryHidden
End If

End Sub