VBA - Hide Multiple Sheets Based On Date

foolishpiano

New Member
Joined
Aug 19, 2016
Messages
25
Hello everyone! I currently have a workbook that is updated daily, and I've created a macro that captures the data and creates a new worksheet in the workbook dated with the current date. I've included what I think is all of the relevant code below (I'm happy to post the full code if that would be helpful as well):

Code:
' Create new worksheet with today's date
        Dim szTodayDate As String
        szTodayDate = Format(Date, "mmm-dd-yyyy")
        On Error GoTo MakeSheet
        Sheets(szTodayDate).Activate
        Exit Sub
MakeSheet:
        Sheets.Add , Worksheets(Worksheets.count)
        ActiveSheet.Name = szTodayDate
' Copy table data from FTS to new worksheet
    Worksheets("Table Data from FTS").ListObjects("Table_query__1").Range.Copy
    ActiveSheet.Range("A1").PasteSpecial Paste:=xlPasteValues

What I'm wondering is: is it possible to add to the code a line that will go through the workbook and hide all worksheets that were created over a week ago? The number of open worksheets is starting to become ridiculous, and does not need to be visible all of the time. This data is just for historical record to be recalled if needed.

Thank you all for your help!
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
If you normal date format is MM/DD/YYYY then you could try
Code:
Dim ws As Worksheet
For Each ws In Worksheets
   If ws.name < Date - 7 Then
      ws.Visible = False
   End If
Next ws
 
Upvote 0
Thank you so much for your reply, Fluff! Your code worked a little too well; all of the worksheets except the new one were hidden. My code is now:

Code:
' Turn off screen updating
    Application.ScreenUpdating = False
' Hide worksheets older than seven days ago
    Dim ws As Worksheet
    For Each ws In Worksheets
        If ws.Name < Date - 7 Then
        ws.Visible = False
    End If
    Next ws
    ' Create new worksheet with today's date
        Dim szTodayDate As String
        szTodayDate = Format(Date, "mm-dd-yyyy")
        On Error GoTo MakeSheet
        Sheets(szTodayDate).Activate
        Exit Sub
MakeSheet:
        Sheets.Add , Worksheets(Worksheets.count)
        ActiveSheet.Name = szTodayDate
' Copy table data from FTS to new worksheet
    Worksheets("Table Data from FTS").ListObjects("Table_query__1").Range.Copy
    ActiveSheet.Range("A1").PasteSpecial Paste:=xlPasteValues

Any idea why they all hide themselves?
 
Upvote 0
What is your standard date format?
 
Upvote 0
"mm-dd-yyyy" is how my worksheets are named, i.e. 06-25-2018, 06-22-2018, 06-21-2018, etc.. I would include a screenshot of the workbook, but I apologize I'm not sure how.
 
Upvote 0
Is MM/DD/YYYY your regional date format, or do you use a European style date format of DD/MM/YYYY
 
Upvote 0
Not sure why that happens, with UK dates if I have sheet names 17-06-2018 & 19-06-2018 then the 1st sheet is hidden but the other isn't.
Try adding this msgbox
Code:
   If ws.name < Date - 7 Then
     [COLOR=#0000ff] MsgBox CLng(CDate(ws.name)) & vbLf & CLng(Date - 7)[/COLOR]
      ws.Visible = False
   End If
What happens?
 
Upvote 0
The message box returned:

43256
43269

then
43257
43269

then
43258
43269

then
43259
43269

then
43262
43269

then
43264
43269

then
43265
43269

then
43266
43269

then
43269
43269

then
43271
43269

then
43272
43269

then
43273
43269

To my eyes it appears as though it's checking the dates correctly.
 
Upvote 0
Odd, How about
Code:
Dim ws As Worksheet
For Each ws In Worksheets
   If IsDate(ws.name) Then
      If CDate(ws.name) < Date - 7 Then ws.Visible = False
   End If
Next ws
 
Upvote 0

Forum statistics

Threads
1,214,924
Messages
6,122,294
Members
449,077
Latest member
Rkmenon

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