Macro - Find a cell and hide columns

CJG19

New Member
Joined
Jul 12, 2021
Messages
16
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

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
59,025
Office Version
  1. 365
Platform
  1. Windows
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")
 
Solution

CJG19

New Member
Joined
Jul 12, 2021
Messages
16
Office Version
  1. 2010
Platform
  1. Windows
Thank you Joe4, it works perfectly for what I need!
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
59,025
Office Version
  1. 365
Platform
  1. Windows
You are welcome.
Glad I was able to help!
:)
 

Forum statistics

Threads
1,141,413
Messages
5,706,301
Members
421,441
Latest member
VapesRub

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
Top