隐藏多列VBA Excel

时间:2018-11-09 12:20:09

标签: excel vba excel-vba hide multiple-columns

在工作表1中,我需要显示(C:N)中的所有列,其中在(B2)中选择月份,其他列必须隐藏。如果我在B2中选择了第4个月,那么我只需要查看这些列,即(C:N)中第4个月的日期等等。

如果我有完整的日期日历,如何找到确切的月份并隐藏其他月份?

enter image description here

Private Sub Worksheet_Change(ByVal Target As Range)
Dim wb As Workbook
Dim ws As Worksheet
Dim entireRange As Range
Set wb = ThisWorkbook
Set ws = wb.Sheets("Sheet1")
Set entireRange = ws.Columns("C:N")

entireRange.EntireColumn.Hidden = False

Select Case ws.Range("B2")

  Case "1"
      ws.Range("G:N").EntireColumn.Hidden = True

  Case "5"
      ws.Range("C:F,H:N").EntireColumn.Hidden = True

  Case "6"
      ws.Range("C:G,I:N").EntireColumn.Hidden = True



End Select
End Sub

1 个答案:

答案 0 :(得分:1)

一个简单的代码可以隐藏不适合您月份的列,如下所示:

Sub HidingColumn()

Dim i As Long

For i = 12 To 3 Step -1 '12 being your N Column

    If Month(Cells(4, i)) <> Range("B2").Value Then
        Cells(4, i).Columns.Hidden = True
    End If
Next i

End Sub