hide columns based on dates

domtrump

Board Regular
Joined
Apr 1, 2010
Messages
245
I have a report with dates as column headings. How would I write VBA to Hide all columns in a range that were

a. more than two days in the past from today AND
b. more than 30 days in the future from today

Trying to produce a snapshot for management and hide the rest.

Any ideas?

Thanks.
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
There's probably many cleaner ways of doing this but try;
Code:
Sub hide_Columns()

history_date = Format(Now() - 2, "d/mm/yyyy")
future_date = Format(Now() + 30, "d/mm/yyyy")

For Each date_column In Me.Range("A1:BB1")
   If DateValue(date_column) < DateValue(history_date) _
   Or DateValue(date_column) > DateValue(future_date) Then
    date_column.EntireColumn.Hidden = True
   Else
    date_column.EntireColumn.Hidden = False
   End If
Next date_column

End Sub
 
Upvote 0
There's probably many cleaner ways of doing this but try;
Code:
Sub hide_Columns()
 
history_date = Format(Now() - 2, "d/mm/yyyy")
future_date = Format(Now() + 30, "d/mm/yyyy")
 
For Each date_column In Me.Range("A1:BB1")
   If DateValue(date_column) < DateValue(history_date) _
   Or DateValue(date_column) > DateValue(future_date) Then
    date_column.EntireColumn.Hidden = True
   Else
    date_column.EntireColumn.Hidden = False
   End If
Next date_column
 
End Sub

Thanks - I'll give it a try!
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,283
Members
452,902
Latest member
Knuddeluff

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