Macro - Find a cell and hide columns

CJG19

New Member
Joined
Jul 12, 2021
Messages
40
Office Version
  1. 2010
Platform
  1. Windows
Good Morning,

I am new to Macros and I am having an issue with one I am trying to write my second at the moment.

I have months at the top of my columns from Jan 2021 to Dec 2026. I want to have a button so I can hide all previous months and all future months, just showing the current month. I thought maybe some sort of marker above the current month, but I am not sure that would work. My other idea is base it on today's date?

Any help would be appreciated.

Many Thanks!

CJG19
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Welcome to the Board!

If your title row with months is actually entered as valid dates, this VBA code should do what you want:
VBA Code:
Sub HideColumns()

    Dim rng As Range
    Dim cell As Range
    
'   Designate which cells to check (all in one row)
    Set rng = Range("A1:L1")
    
'   First, unhide all columns
    rng.EntireColumn.Hidden = False
    
'   Loop through cells and hide columns
    For Each cell In rng
'       Check to see if current month/year
        If Format(cell, "mmyy") <> Format(Date, "mmyy") Then
'           Hide if not equal
            cell.EntireColumn.Hidden = True
        End If
    Next cell
    
End Sub
Just change this line to match the cells containing your date headers:
Set rng = Range("A1:L1")
 
Upvote 0
Solution
Thank you Joe4, it works perfectly for what I need!
 
Upvote 0
You are welcome.
Glad I was able to help!
:)
 
Upvote 0

Forum statistics

Threads
1,214,560
Messages
6,120,217
Members
448,951
Latest member
jennlynn

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