选择"多列"以一次隐藏



我正在运行一份报告,其中有多个列来比较整个财政年度的数字。例如,7月至8月,8月至9月等。不幸的是,当我手动执行此操作时,我必须取消隐藏所有列并重新隐藏未用于完成的列。我正在自动化此报告。此时,我的所有列都可见。由于处于财政年度中期,我的报表将在所需数据的左侧和右侧都有列。我正在尝试隐藏这些不需要的列。

我已经创建了一个引用行(第 1 行(,其中包含 JAN、FEB、MAR 等,仅用于我的代码,因为我创建了一个检测这些条目的用户表单。

对于我本月需要的数据,我选择了引用的单元格并将其偏移 1,以选择我需要隐藏的第一列。如何在脚本中选择从所选列到列 A 的所有列?以下是我所在位置的代码:

With Worksheets("ACD")
For counter = 1 To 200
Set curcell = Worksheets("ACD").Cells(1, counter)
If curcell.Value = FormMonth Then
curcell.Offset(0, -1).EntireColumn.Select
End If
Next counter
End With

感谢您的帮助!

下面是有关如何根据您的情况隐藏多个列的示例:

Option Explicit
Public Sub HideMonthColumns()
Dim workingDate As Date                      'filled in from your form?
workingDate = #10/1/2018#                    'set for testing here
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("ACD")
Dim lastCol As Long
Dim workingCol As Long
Dim i As Long
With ws
lastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
For i = 1 To lastCol
If .Cells(1, i) = workingDate Then
workingCol = i
Exit For
End If
Next i
'--- hide all the columns to the left and right of the working column
.Range(.Cells(1, 1), .Cells(1, workingCol - 1)).EntireColumn.Hidden = True
'--- ... and hide all the columns to the right, checking for the edge
If (workingCol + 9) < lastCol Then
.Range(.Cells(1, workingCol + 9), _
.Cells(1, lastCol)).EntireColumn.Hidden = True
End If
End With
End Sub

最新更新