Show (UnHide) Matching Columns
Usage
Sub ShowJan() ShowMonth 1 ' maybe "1"? End Sub
The Method
Sub ShowMonth(ByVal MonthToShow As Variant) ' Define constants. Const PASTE_RANGE_ADDRESS As String = "D1:ABG1" Const COPY_ROW_OFFSET As Long = 1 ' Reference the worksheet. Dim ws As Worksheet: Set ws = ActiveSheet ' improve! ' Reference the range. Dim rg As Range: Set rg = ws.Range(PASTE_RANGE_ADDRESS) ' Return 1 for each matching column in a 1D one-based array. Dim ColumnIndices() As Variant With rg ' Copy values (from the row below). .Value = .Offset(COPY_ROW_OFFSET).Value ' Format font. With .Font .ThemeColor = xlThemeColorDark1 .TintAndShade = 0 End With ' Match columns. ColumnIndices = Application.Match(.Cells, Array(MonthToShow), 0) End With ' Loop through the array and for all values equaling 1 ' (not equaling an error), add the corresponding cell to a unioned range. Dim urg As Range, c As Long For c = 1 To UBound(ColumnIndices) If IsNumeric(ColumnIndices(c)) Then If urg Is Nothing Then Set urg = rg.Cells(c) Else Set urg = Union(urg, rg.Cells(c)) End If End If Next c ' Hide all entire columns of the range. rg.EntireColumn.Hidden = True ' Show the matching columns. If urg Is Nothing Then MsgBox "No columns for month " & MonthToShow & "!", vbExclamation Else urg.EntireColumn.Hidden = False MsgBox "Showing columns for month " & MonthToShow & ".", vbInformation End If End Sub
Application.Sreenupdating = Falseshould speed things up. You also don’t need to check every cell in that row, only the used part.