英文:
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
' Check if the value of cell B1 has changed
If Target.Address = "$B$1" Then
' Check the new value of cell B1
If Target.Value = "HIDE" Then
hideFtoJ = True
ElseIf Target.Value = "SHOW" Then
hideFtoJ = False
End If
End If
' Check if the value of cell B2 has changed
If Target.Address = "$B$2" Then
' Check the new value of cell B2
If Target.Value = "HIDE" Then
hideKtoL = True
ElseIf Target.Value = "SHOW" Then
hideKtoL = False
End If
End If
' Check if the value of cell B3 has changed
If Target.Address = "$B$3" Then
' Check the new value of cell B3
If Target.Value = "HIDE" Then
hideMtoS = True
ElseIf Target.Value = "SHOW" Then
hideMtoS = False
End If
End If
' Loop through all the worksheets in the workbook
For Each ws In ThisWorkbook.Worksheets
If ws.Name <> "Sheet1" Then
' Hide or unhide columns F to J based on the value of B1
If hideFtoJ = True Then
ws.Range("F:J").EntireColumn.Hidden = True
Else
ws.Range("F:J").EntireColumn.Hidden = False
End If
' Hide or unhide columns K to L based on the value of B2
If hideKtoL = True Then
ws.Range("K:L").EntireColumn.Hidden = True
Else
ws.Range("K:L").EntireColumn.Hidden = False
End If
' Hide or unhide columns M to S based on the value of B3
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
答案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("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
答案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("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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论