英文:
Hide activecell column across tabs using VBA
问题
我正在尝试隐藏前4个选项卡中活动单元格所在的列。
我选择突出显示前4个选项卡,然后选择单元格A1,然后使用以下代码:
ActiveCell.EntireColumn.Hidden = True
这会隐藏第一个选项卡中的列A,但不会隐藏选项卡2、3和4中的列。
必须是所选单元格,而不是Range("A1").EntireColumn.Hidden = True
,因为单元格可以使用偏移函数来隐藏。
英文:
I am trying to hide the activecell column across first 4 tabs.
I select highlight first 4 tabs and select cell A1 then use below code:
ActiveCell.EntireColumn.Hidden = True
This hides column A in first tab, but not tab 2, 3, 4.
It has to be the selected cell and not Range("A1").EntireColumn.Hidden = True
, because the cell can be any active cell I am trying to hide using an offset function.
答案1
得分: 1
你可以存储活动单元格的位置,然后循环遍历每个工作表,并使用该位置来隐藏相应的列。
Sub tested()
Dim cell_address As String
Dim wsName, ws, c As Range
cell_address = ActiveCell.Address
wsName = Array("Sheet1", "Sheet2", "Sheet3", "Sheet4")
For Each ws In wsName
Sheets(ws).Range(cell_address).EntireColumn.Hidden = True
Next
End Sub
英文:
You could store the location of the active cell, then cycle through each worksheet and use that location to base your column hiding..
Sub tested()
Dim cell_address As String
Dim wsName, ws, c As Range
cell_address = ActiveCell.Address
wsName = Array("Sheet1", "Sheet2", "Sheet3", "Sheet4")
For Each ws In wsName
Sheets(ws).Range(cell_address).EntireColumn.Hidden = True
Next
End Sub
答案2
得分: 0
隐藏不同工作表中相同的列
- 没有办法同时从多个工作表中组合(选择)单元格,因此您需要循环遍历工作表,并逐个隐藏每列。
用法
Sub HideCellColumnsTEST()
Dim wsIDs(): wsIDs = Array(1, 2, 3, 4)
Dim ws As Worksheet: Set ws = ActiveSheet
Dim cell As Range: Set cell = ws.Range("A1")
'或者:
'Set cell = ActiveCell
'Set cell = ActiveCell.Offset(, 3) '...等等。
HideCellColumns cell, wsIDs
End Sub
方法
Sub HideCellColumns(ByVal cell As Range, ByVal wsIDs As Variant)
Dim CellAddress As String: CellAddress = cell.Address
Dim wb As Workbook: Set wb = cell.Worksheet.Parent
Dim ws As Worksheet, wsID
For Each wsID In wsIDs
On Error Resume Next
Set ws = wb.Worksheets(wsID)
On Error GoTo 0
If Not ws Is Nothing Then ' 工作表存在
ws.Range(CellAddress).EntireColumn.Hidden = True
Set ws = Nothing ' 为下一次迭代重置
'Else ' 工作表不存在; 什么也不做!?
End If
Next wsID
End Sub
英文:
Hide the Same Column in Different Worksheets
- There is no way to combine (select) cells from multiple worksheets at the same time so you need to loop through the worksheets and hide each column separately, one after the other.
Usage
<!-- language: lang-vb -->
Sub HideCellColumnsTEST()
Dim wsIDs(): wsIDs = Array(1, 2, 3, 4)
Dim ws As Worksheet: Set ws = ActiveSheet
Dim cell As Range: Set cell = ws.Range("A1")
'or:
'Set cell = ActiveCell
'Set cell = ActiveCell.Offset(, 3) '...etc.
HideCellColumns cell, wsIDs
End Sub
The Method
<!-- language: lang-vb -->
Sub HideCellColumns(ByVal cell As Range, ByVal wsIDs As Variant)
Dim CellAddress As String: CellAddress = cell.Address
Dim wb As Workbook: Set wb = cell.Worksheet.Parent
Dim ws As Worksheet, wsID
For Each wsID In wsIDs
On Error Resume Next
Set ws = wb.Worksheets(wsID)
On Error GoTo 0
If Not ws Is Nothing Then ' worksheet exists
ws.Range(CellAddress).EntireColumn.Hidden = True
Set ws = Nothing ' reset for the next iteration
'Else ' worksheet doesn't exist; do nothing!?
End If
Next wsID
End Sub
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论