Unhide Columns based on Month Range

xMIKExSMITHx

New Member
Joined
Jul 11, 2014
Messages
45
Without getting too into the details, I have my 2023 forecast file macro which hides all the columns for 2024 forecasting(BG:BT), but since we are approaching 4th qtr, I need to keep the columns unhidden. My thought was to insert a few more lines of code that say if current month (I want use month instead of dates so I dont have to go and update the code each year to trigger the macro) is Aug, Sep, Oct, Nov, Dec, or Jan, unhide columns BG:BT on each sheet. I was trying to use the Application.OnTime function but hitting roadblocks on how to properly use it with only months and not dates, and also maybe thinking an "IF" function might be better suited for this task. Any help is appreciated.
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
put this code into Workbook_Open event
in spreadsheet, enter VBE (alt-F11)
in left pane (projects) : dbl-click the ThisWorkbook object
in right pane (code side): paste the code.

when workbook opens the event will fire.

Code:
Private Sub Workbook_Open()
HideCols
End Sub


Private Sub HideCols()
'hide the bg:bt columns
Select Case Month(Date)
   Case 8, 9, 10, 11, 12
     Columns("BG:BT").Hidden = False
   Case Else
     Columns("BG:BT").Hidden = True
End Select
End Sub
 
Upvote 0
Thank you for the quick reply ranman256. I didnt get a chance to try your solution because I took my own advice and just wrote an If/Or function as such which fixed the issue:

VBA Code:
'This collapses the next year columns except for during the months of August through January
    
   ActiveSheet.Outline.ShowLevels RowLevels:=0, ColumnLevels:=1
    
    
    ActiveSheet.EnableOutlining = True

    Columns("H:H").ColumnWidth = CommnentWidth
    
    Columns("M:M").EntireColumn.AutoFit
    
    Rows("6:6").RowHeight = HeaderHeight
    
    
 If Month(Now) = 1 Or Month(Now) = 8 Or Month(Now) = 9 Or Month(Now) = 10 Or Month(Now) = 11 Or Month(Now) = 12 Then

    Columns("BG:BT").Select
    Range("BT1").Activate
    Selection.EntireColumn.Hidden = False
    Columns("AF:AS").Select
    Range("AS1").Activate
    Selection.EntireColumn.Hidden = False
    Range("A1").Select

Else

End If
 
Upvote 0
Solution
Note that you could shorten up this a little:
VBA Code:
 If Month(Now) = 1 Or Month(Now) = 8 Or Month(Now) = 9 Or Month(Now) = 10 Or Month(Now) = 11 Or Month(Now) = 12 Then

    Columns("BG:BT").Select
    Range("BT1").Activate
    Selection.EntireColumn.Hidden = False
    Columns("AF:AS").Select
    Range("AS1").Activate
    Selection.EntireColumn.Hidden = False
    Range("A1").Select

Else

End If
like this:
VBA Code:
Select Case Month(Now)
    Case 1, 8, 9, 10, 11, 12
        Columns("BG:BT").Select
        Range("BT1").Activate
        Selection.EntireColumn.Hidden = False
        Columns("AF:AS").Select
        Range("AS1").Activate
        Selection.EntireColumn.Hidden = False
        Range("A1").Select
End Select
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,286
Members
452,631
Latest member
a_potato

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top