VBA on workbook open - Show hidden columns if cell contains date < now

B-Man

Board Regular
Joined
Dec 29, 2012
Messages
183
Office Version
  1. 2019
Platform
  1. Windows
I have created a workbook and pre-filled some columns with data but want it hidden unless it hits the year it numbered as.
i have row 2 as the headers with the dates (albeit merged) and row 4 = row 2 dates (in each cell rather than a merged cell) for each column (un-merged)
I have hidden columns H:AM
on workbook open if those columns are older than todays date i want to unhide them.

i currently use this below but get "xCell.Value=empty" causing the vba to break. what am i doing wrong?

VBA Code:
Private Sub Workbook_Open()

Dim xCell As Range
    For Each xCell In Range("H4:AM4")
        If xCell.Value < Date Then
            xCell.EntireColumn.Show = True
        End If
    Next
End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Sounds like some of the cells from H4 to AM4 do not contain dates.
What do you want to do if the cell is empty?
 
Upvote 0
Sounds like some of the cells from H4 to AM4 do not contain dates.
What do you want to do if the cell is empty?
I added dates to the blank cells and now i get "run time error 424 object required"
 
Upvote 0
It should be .Hidden = False rather than .Show = True
 
Upvote 0
Solution
Ok that sort of works...
It has unhidden all the columns.
not just the ones with the date < Date.
The date is 1/1/2020, 1/1/2021 etc. But formatted to show just the year. The cells in row 4 are a formula ie c4=c2 d4=c2 e4=c2
 
Upvote 0
Are your dates real dates, or text?
 
Upvote 0
My dates are real dates with custom formatting yyyy
The cells refer to that so end up being 2020 not 1/1/2020
 
Upvote 0
If H4:AM4 all have valid dates, I'm not sure why it unhides every column, as it doesn't do that for me, regardless of how I format the dates.
 
Upvote 0
I checked it and the formatting had reverted to general. Changed it back to dates and works a treat. Thanks.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,932
Messages
6,122,323
Members
449,077
Latest member
jmsotelo

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