Date function recognize current month and year

D_Holmes

New Member
Joined
Sep 18, 2020
Messages
32
Office Version
  1. 2013
Platform
  1. Windows
Hi All,

I am trying to write a vba code that will read a date on the current sheet and if that date does not fall within the current month and year, said sheet will be hidden. So far I have:

If Worksheets("09_2020").Range("A2").Value <> Format(Date, "MM/DD/YYYY") Then

Worksheets("09_2020").Visible = False

End If

All help is welcomed. Thank you.
 
Thank you so much. The code works! I somehow messed up everything else, but I will troubleshoot it.
 
Upvote 0

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Last question. If i wanted to use this formula to go through multiple sheets, how would I write this?
 
Upvote 0
Something like this:
VBA Code:
Dim ws as Worksheet
For each ws in Worksheets
    If Format(ws.Range("A2"),"mmyyyy") <> Format(Date, "mmyyyy") Then
        ws.Visible = False
    Else 
        ws.Visible = True
    End If
Next ws
 
Upvote 0
Just wondering... is it at all possible that the month-year on all sheets will not match the current date's month-year? If so, what should happen (at least one sheet must be visible)?
 
Upvote 0
I wrote the code individually for all of the sheets I want it to run on. The other sheets will remain untouched in my workbook.
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,561
Members
449,089
Latest member
Motoracer88

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