根据列表隐藏工作表

Hiding worksheet based on a list

嘿,有一个简单的问题,我正在编写一个代码,它根据工作表名称旁边的字段是或否来隐藏工作表列表。因此,我有 29 个工作表的列表,我希望我的代码查看该名称旁边的字段,如果是,则显示它,如果不是,它将隐藏它。

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim ws As Worksheet

    For Each ws In Worksheets
        If ws.Name = Worksheets("Settings").Range("B4:B32") _
        And Worksheets("Setting").Range("C4:C32") = "Yes" Then
            ws.Visible = True
        End If
        If ws.Name = Worksheets("Settings").Range("B4:B32") _
        And Worksheets("Setting").Range("C4:C32") = "No" Then
            ws.Visible = True
        End If
    Next ws
End Sub

我运行它并不断收到不匹配错误我是编程新手所以我认为我没有正确调用东西

我相信下面的代码应该能满足您的需要:

Private Sub Worksheet_Change(ByVal Target As Range)

    Dim wsSettings As Worksheet
    Dim wsHideShow As Worksheet
    Dim rngSheets As Range
    Dim xlCell As Range
    Dim sheetName As String

    Set wsSettings = ThisWorkbook.Worksheets("Settings")
    Set rngSheets = wsSettings.Range("B4:B32")

    For Each xlCell In rngSheets
        sheetName = xlCell.Value

        If sheetName <> "" Then

            Set wsHideShow = ThisWorkbook.Worksheets(sheetName)

            If xlCell.Offset(0, 1).Value = "yes" Then
                wsHideShow.Visible = False
            Else
                wsHideShow.Visible = True
            End If
        End If
    Next xlCell

End Sub

与其循环遍历您的工作表,不如循环遍历您的列表并相应地 hide/show 工作表。

打字慢...但是...我的建议是:

Private Sub WorkSheet_Change(ByVal Target as range)

   If (Target.Row >= 4 And Target.Row <= 32 And Target.Column = 2) Then
      Dim i as Integer
      For i=0 To 28 Step 1
         If Range("B" & 2 + i).Value = "YES" Then
           ThisWorkBook.Worksheets(Range("A" & 2 + i).Value).Visible = True
         Else
           ThisWorkBook.Worksheets(Range("A" & 2 + i).Value).Visible = False
         End If
      Next i
   End If

End Sub

仅当 sheet 上的 B4:B32 范围内的值更改时才会触发...

希望这对您有所帮助...

此代码在标准模块中运行。它假设master是Settings而不是Setting

它遍历 table 以及工作表:

Sub DisplayOrHideSheets()
    Dim sh As Worksheet
    For Each ws In Sheets
        v = ws.Name
        For Each r In Worksheets("Settings").Range("B4:B32")
            If r.Value = v Then
                If r.Offset(0, 1) = "Yes" Then
                    ws.Visible = True
                Else
                    ws.Visible = False
                End If
            End If
        Next r
    Next ws
End Sub