Macro to hide all columns that are before current month

tonywatsonhelp

Well-known Member
Joined
Feb 24, 2014
Messages
3,210
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hi Everyone,

I have a ist of columns with dates in row 20 full range is "AB20:DXX20"

All i want to do is have a macro that when i run it Hide all dates before todays month so right now its 11 may so everying before 1st may

please help if you can

Thanks

Tony
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Are the dates numbers or text?
What's the time increment between adjacent cells?
 
Upvote 0
If the dates are consecutive then this might work.

Code:
Sub t2()
Dim fn As Range, dt As Date
    dt = Application.EoMonth(Date, -1)
    With ActiveSheet
        .Range("AB:DXX").EntireColumn.Hidden = False
        MsgBox dt
        Set fn = .Range("AB20", .Cells(20, Columns.Count).End(xlToLeft)).Find(dt, , xlValues)
            If Not fn Is Nothing Then
                .Range("AB20", fn).EntireColumn.Hidden = True
            End If
    End With
End Sub

Otherwise, you might need something like this

Code:
Sub t()
Dim c As Range
    With ActiveSheet
        .Range("AB:DXX").EntireColumn.Hidden = False
        For Each c In .Range("AB20", .Cells(20, Columns.Count).End(xlToLeft))
            If c.Value < Date And Month(c.Value) <> Month(Date) Then
                c.EntireColumn.Hidden = True
            End If
        Next
    End With
End Sub
 
Upvote 0
Assuming dates are numbers in ascending order and increment is one day between adjacent cells, here's another non-looping solution:
Code:
Sub HidePriorMonths()
Dim R As Range, Adr, LastCol As Long
Set R = Range("AB20:DXX20")
Application.ScreenUpdating = False
R.Columns.Hidden = False
Adr = [MATCH(TODAY(),AB20:DXX20,0)]
LastCol = R.Columns(Adr - Day(Date)).Column
Range(R.Cells(1, 1), Cells(R.Row, LastCol)).EntireColumn.Hidden = True
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,632
Latest member
jladair

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