VBA隐藏多个工作表上的多个列。

huangapple go评论77阅读模式
英文:

VBA to hide multiple columns on multiple sheets

问题

我有一个包含6个工作表的工作簿。工作表2至5包含我想要隐藏或显示的列。工作表6包含键,我不想对其进行更改。在工作表1上,我有A列和B列。A列包含在工作表2至5上存在的标题名称。B列中有一个"隐藏/显示"的下拉菜单。我尝试编写一个宏来实现这一目标,但我的连续的If语句出现了if块错误消息。非常感激任何帮助。我希望最好能使用事件处理程序,以便当工作表1上B1到B8单元格中的值发生更改时,代码会自动运行。我尝试将以下代码插入为模块和专门针对工作表1的代码,但都没有成功。数据集都不是数组。

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim ws As Worksheet
    Dim hideFtoJ As Boolean, hideKtoL As Boolean, hideMtoS As Boolean
    
    ' 检查单元格B1的值是否已更改
    If Target.Address = "$B$1" Then
        ' 检查单元格B1的新值
        If Target.Value = "隐藏" Then
            hideFtoJ = True
        ElseIf Target.Value = "显示" Then
            hideFtoJ = False
        End If
    End If
    
    ' 检查单元格B2的值是否已更改
    If Target.Address = "$B$2" Then
        ' 检查单元格B2的新值
        If Target.Value = "隐藏" Then
            hideKtoL = True
        ElseIf Target.Value = "显示" Then
            hideKtoL = False
        End If
    End If
    
    ' 检查单元格B3的值是否已更改
    If Target.Address = "$B$3" Then
        ' 检查单元格B3的新值
        If Target.Value = "隐藏" Then
            hideMtoS = True
        ElseIf Target.Value = "显示" Then
            hideMtoS = False
        End If
    End If
    
    ' 循环遍历工作簿中的所有工作表
    For Each ws In ThisWorkbook.Worksheets
        If ws.Name <> "Sheet1" Then
            ' 根据B1的值隐藏或显示列F到J
            If hideFtoJ = True Then
                ws.Range("F:J").EntireColumn.Hidden = True
            Else
                ws.Range("F:J").EntireColumn.Hidden = False
            End If
            
            ' 根据B2的值隐藏或显示列K到L
            If hideKtoL = True Then
                ws.Range("K:L").EntireColumn.Hidden = True
            Else
                ws.Range("K:L").EntireColumn.Hidden = False
            End If
            
            ' 根据B3的值隐藏或显示列M到S
            If hideMtoS = True Then
                ws.Range("M:S").EntireColumn.Hidden = True
            Else
                ws.Range("M:S").EntireColumn.Hidden = False
            End If
        End If
    Next ws
End Sub

希望这对你有所帮助。

英文:

I have a workbook with 6 sheets. Sheets 2-5 contain columns I want to hide or unhide. Sheet 6 contains keys and I don't want to alter it. On sheet 1 I have columns A and B. Column A contains the names of headers which are present on sheets 2-5. In column B is a drop down for "hide/unhide". I have tried to write a macro to achieve this, but my sequential If statements error with an if block error message. Any help gratefully received. I ideally want to use an event handler so that the code runs when the values in B1 to B8 on Sheet 1 are changed, automatically. I have tried inserting the below code as both a module, and as code specific to Sheet 1, but no success. None of the data sets are in arrays.

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim ws As Worksheet
    Dim hideFtoJ As Boolean, hideKtoL As Boolean, hideMtoS As Boolean
    
    &#39; Check if the value of cell B1 has changed
    If Target.Address = &quot;$B$1&quot; Then
        &#39; Check the new value of cell B1
        If Target.Value = &quot;HIDE&quot; Then
            hideFtoJ = True
        ElseIf Target.Value = &quot;SHOW&quot; Then
            hideFtoJ = False
        End If
    End If
    
    &#39; Check if the value of cell B2 has changed
    If Target.Address = &quot;$B$2&quot; Then
        &#39; Check the new value of cell B2
        If Target.Value = &quot;HIDE&quot; Then
            hideKtoL = True
        ElseIf Target.Value = &quot;SHOW&quot; Then
            hideKtoL = False
        End If
    End If
    
    &#39; Check if the value of cell B3 has changed
    If Target.Address = &quot;$B$3&quot; Then
        &#39; Check the new value of cell B3
        If Target.Value = &quot;HIDE&quot; Then
            hideMtoS = True
        ElseIf Target.Value = &quot;SHOW&quot; Then
            hideMtoS = False
        End If
    End If
    
    &#39; Loop through all the worksheets in the workbook
    For Each ws In ThisWorkbook.Worksheets
        If ws.Name &lt;&gt; &quot;Sheet1&quot; Then
            &#39; Hide or unhide columns F to J based on the value of B1
            If hideFtoJ = True Then
                ws.Range(&quot;F:J&quot;).EntireColumn.Hidden = True
            Else
                ws.Range(&quot;F:J&quot;).EntireColumn.Hidden = False
            End If
            
            &#39; Hide or unhide columns K to L based on the value of B2
            If hideKtoL = True Then
                ws.Range(&quot;K:L&quot;).EntireColumn.Hidden = True
            Else
                ws.Range(&quot;K:L&quot;).EntireColumn.Hidden = False
            End If
            
            &#39; Hide or unhide columns M to S based on the value of B3
            If hideMtoS = True Then
                ws.Range(&quot;M:S&quot;).EntireColumn.Hidden = True
            Else
                ws.Range(&quot;M:S&quot;).EntireColumn.Hidden = False
            End If
        End If
    Next ws
End Sub

答案1

得分: 1

私人子过程 Worksheet_Change(ByVal Target As Range) 会在工作表更改时隐藏其他工作表中的列。

以下是代码的翻译:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim tCells(): tCells = Array("B1", "B2", "B3")
    Dim HideCols(): HideCols = Array("F:J", "K:L", "M:S")
    Dim SheetIndexes(): SheetIndexes = Array(2, 3, 4, 5)
    
    If Target.Cells.CountLarge > 1 Then Exit Sub
    
    Dim n As Long
    
    For n = LBound(tCells) To UBound(tCells)
        If Not Intersect(Me.Range(tCells(n)), Target) Is Nothing Then Exit For
    Next n
    
    If n > UBound(tCells) Then Exit Sub
    
    Dim hCols As String: hCols = HideCols(n)
    Dim IsHidden As Boolean
    IsHidden = StrComp(CStr(Target.Value), "HIDE", vbTextCompare) = 0
    
    For n = LBound(SheetIndexes) To UBound(SheetIndexes)
        Me.Parent.Worksheets(SheetIndexes(n)).Columns(hCols).Hidden = IsHidden
    Next n
End Sub
英文:

A Worksheet Change: Hide Columns in Other Worksheets

<!-- language: lang-vb -->

Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim tCells(): tCells = Array(&quot;B1&quot;, &quot;B2&quot;, &quot;B3&quot;)
    Dim HideCols(): HideCols = Array(&quot;F:J&quot;, &quot;K:L&quot;, &quot;M:S&quot;)
    
    Dim SheetIndexes(): SheetIndexes = Array(2, 3, 4, 5)
    
    If Target.Cells.CountLarge &gt; 1 Then Exit Sub
    
    Dim n As Long
    
    For n = LBound(tCells) To UBound(tCells)
        If Not Intersect(Me.Range(tCells(n)), Target) Is Nothing Then Exit For
    Next n
    If n &gt; UBound(tCells) Then Exit Sub
    
    Dim hCols As String: hCols = HideCols(n)
    Dim IsHidden As Boolean
    IsHidden = StrComp(CStr(Target.Value), &quot;HIDE&quot;, vbTextCompare) = 0
    
    For n = LBound(SheetIndexes) To UBound(SheetIndexes)
        Me.Parent.Worksheets(SheetIndexes(n)).Columns(hCols).Hidden = IsHidden
    Next n
    
End Sub

答案2

得分: 0

请将以下代码复制到Sheet1的代码模块中。您没有回答我关于在范围“B4:B8”中更改情况下要处理的列的澄清问题。请将特定数组从第四个元素调整到第八个元素:

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
     Dim rngTg As Range: Set rngTg = Me.Range("B1:B8")
     
     If Not Intersect(Target, rngTg) Is Nothing Then
        'columns ranges from the fourth one should be adapted to your needs:
        Dim arrCols(): arrCols = Array("F:J", "K:L", "M:S", "T:V", "W:Z", "AA:AC", "AD:AE", "AF:AH")
        If Target.Value = "HIDE" Then
            hideCols CStr(arrCols(Target.Column - 1)), True
        ElseIf Target.Value = "SHOW" Then
            hideCols CStr(arrCols(Target.Column - 1)), False
        End If
     End If
End Sub

Sub hideCols(strCols As String, boolVis As Boolean)
    Dim arrSheets(): arrSheets = Array(2, 3, 4, 5) 'array of sheets to be processed
    Dim sh As Worksheet
    For Each sh In Worksheets(arrSheets)
        sh.Range(strCols).EntireColumn = boolVis
    Next sh
End Sub

请注意,这是您提供的代码的翻译部分,不包括其他内容。

英文:

Please, copy the next code in Sheet1 code module. You did not answer my clarification question related to columns to be handled for the cases of changes in the range "B4:B8". Please, adapt the specific array from the fourth element to the eighth one:

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
     Dim rngTg As Range: Set rngTg = Me.Range(&quot;B1:B8&quot;)
     
     If Not Intersect(Target, rngTg) Is Nothing Then
        &#39;columns ranges from the fourth one should be adapted to your needs:
        Dim arrCols(): arrCols = Array(&quot;F:J&quot;, &quot;K:L&quot;, &quot;M:S&quot;, &quot;T:V&quot;, &quot;W:Z&quot;, &quot;AA:AC&quot;, &quot;AD:AE&quot;, &quot;AF:AH&quot;)
        If Target.Value = &quot;HIDE&quot; Then
            hideCols CStr(arrCols(Target.column - 1)), True
        ElseIf Target.Value = &quot;SHOW&quot; Then
            hideCols CStr(arrCols(Target.column - 1)), False
        End If
     End If
End Sub

Sub hideCols(strCols As String, boolVis As Boolean)
    Dim arrSheets(): arrSheets = Array(2, 3, 4, 5) &#39;array of sheets to be processed
    Dim sh As Worksheet
    For Each sh In Worksheets(arrSheets)
        sh.Range(strCols).EntireColumn = boolVis
    Next sh
End Sub

huangapple
  • 本文由 发表于 2023年3月7日 01:10:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75653814.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定