Macro to hide columns that are outside current month

asheets

New Member
Joined
Nov 4, 2015
Messages
6
Hello!

I have searched far and wide for the perfect Code. I am very new to the use of VBA and Macros but I dream big when designing spreadsheets.


I have a sheet that has names going down the rows (A7:A257) with dates (month - year) going across the columns (E5:AN5).

I would like to run a macro which looks at each dated column and hides all columns not in the cureent month. (Ex: It's November. Hides months Jan-Oct and Dec)


Many thanks :smile:
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Hi

You can use the following:

Code:
Sub HideMonths()

    Dim i As Long
    
    For i = 5 To 40
        If VBA.Format(Cells(5, i), "mmm-yyyy") <> VBA.Format(Date, "mmm-yyyy") Then
            Columns(i).Hidden = True
        End If
    Next


End Sub

Let me know how it goes.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,226,693
Messages
6,192,463
Members
453,725
Latest member
cvsdatreas

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