使用循环隐藏列和多个工作表

Hide columns and multiple sheets using loop

我目前有一个循环非常适合隐藏基于多个下拉单元格的列。我还想添加代码以根据相同的下拉菜单隐藏工作表,但我不确定如何添加到我的 For Each Cell In Range 以适应它。我已经粘贴了我必须隐藏下面的列的内容。任何帮助将不胜感激。

Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range

For Each cell In Range("$A:$A")

   If cell = "Descriptor 1" Or cell = "Descriptor 2" Then
    Columns("B:F").EntireColumn.Hidden = False
   Exit For
   Else
    Columns("B:F").EntireColumn.Hidden = True
End If
Next Cell

您可以使用 Worksheets("sheet_to_hide").Visible = xlSheetHidden 之类的东西隐藏 sheet 并使用 Worksheets("sheet_to_unhide").Visible = xlSheetVisible 再次取消隐藏它。

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim cell As Range
    Dim HideIt As Boolean

    HideIt = True
    For Each cell In Range("$A:$A")    
       If cell.Value = "Descriptor 1" Or _
          cell.Value = "Descriptor 2" Then
           HideIt = False
           Exit For
       End If
    Next Cell

    If HideIt Then
        Columns("B:F").Hidden = True
        Worksheets("Sheet1").Visible = xlSheetHidden
        Worksheets("Sheet2").Visible = xlSheetHidden
    Else
        Columns("B:F").Hidden = False
        Worksheets("Sheet1").Visible = xlSheetVisible
        Worksheets("Sheet2").Visible = xlSheetVisible
    End If
End Sub

如果要根据 sheet 名称是否出现在您的范围内来隐藏/显示作品sheet,那么我建议进行以下修改:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim cell As Range
    Dim HideIt As Boolean

    'Don't do anything if there was no change to A30:A38
    If Intersect(Target, Range("$A:$A")) Is Nothing Then Exit Sub

    HideIt = True
    For Each cell In Range("$A:$A")
       If cell.Value = "Descriptor 1" Or _
          cell.Value = "Descriptor 2" Then
           HideIt = False
           Exit For
       End If
    Next cell
    Columns("B:F").Hidden = HideIt

    Dim ws As Worksheet
    For Each ws In Worksheets
        If ws.Name <> ActiveSheet.Name Then
            'See if sheet name exists in A30:A38
            'Hide the sheet if doesn't, make it visible if it does
            ws.Visible = Not IsError(Application.Match(ws.Name, Range("$A:$A"), 0))
        End If
    Next
End Sub

@YowE3K 你的代码很棒。但是我遇到了一个问题,即选项卡名称是缩写形式,而我的描述符是完整形式。因此,我采用了您的原始代码,为每个选项卡添加了一个 "HideTab",并将顶行 HideTab = False 切换为 true 并在第 4 行 HideTab 中将其反转(见下文)。我敢肯定有一种更快的方法,但这就像一种魅力。非常感谢您的帮助!你给我指明了正确的方向。

Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range
Dim HideIt As Boolean

HideIt = True
    For Each cell In Range("$A:$A")    
        If cell.Value = "Descriptor 1" Or
        cell.Value = "Descriptor 2" Then
       HideIt = False
       Exit For
   End If
Next Cell
Columns("B:F").EntireColumn.Hidden = True


Dim HideTab1 As Boolean
    HideTab1 = False
    For Each cell In Range("$A:$A")
        If cell = "Descriptor1" Then
            HideTab1 = True
            Exit For
        End If
    Next cell
    Sheets("Desc1").Visible = HideTab1

Dim HideTab2 As Boolean
    HideTab2 = False
    For Each cell In Range("$A:$A")
        If cell = "Descriptor2" Then
            HideTab2 = True
            Exit For
        End If
    Next cell
    Sheets("Desc2").Visible = HideTab2

Dim HideTab3 As Boolean
    HideTab3 = False
    For Each cell In Range("$A:$A")
        If cell = "Descriptor3" Then
            HideTab3 = True
            Exit For
        End If
    Next cell
    Sheets("Desc3").Visible = HideTab3
End Sub