Auto delete worksheet after period of time

traub86

New Member
Joined
Nov 10, 2017
Messages
26
I need a way to have a worksheet auto-delete after a period of 3 years from when it was created. I currently have worksheets being exported from a working document every time I click an export macro button. This copies that worksheet with the date as the name of the tab. Not sure if this can be worked into the vba code or if its a setting within the workbook itself. Any help would be appreciated. Heres the code i have to export:
Code:
Sub Copysht()
    Workbooks("FINAL_JUMP MANIFEST_FINAL.xlsm").Sheets("DZ LOG").Copy before:=Workbooks("DO NOT DELETE_MASTER DZ LOG.xlsm").Sheets(1)
    Workbooks("DO NOT DELETE_MASTER DZ LOG.xlsm").Sheets(1).Name = Format(Date, "DD-MM-YYYY") + " Chalk 1"
End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
This might work. Add it before End Sub.

Code:
For Each sh In Workbooks("DO NOT DELETE MASTER DZ LOG.xlsm").Sheets
    If Len(sh.Name) >= 10 Then
        If Date - CDate(Left(sh.Name, 10)) >= 365 Then
            Application.DisplayAlerts = False
            sh.Delete
            Application.DisplayAlerts = True
        End If
    End If
Next
 
Last edited:
Upvote 0
Ok and if I needed say a different time frame, say 1 day so I can verify if it works, how might I change it to reflect that?
 
Upvote 0
Ok and if I needed say a different time frame, say 1 day so I can verify if it works, how might I change it to reflect that?

I used 365 in the Post #2 code, which is one year. Just change that figure to multiples of 365 or years, multiples of 30 for months or any number for days. Add 1 for any value divisible by 4 to accomodate leap years.
So for three years it would be:
Code:
If Date - CDate(Left(sh.Name, 10)) >= 1095 Then

It is crude, but it works. All the code does is subtract the date used in the sheet name from the current date which produces a value in days.
 
Last edited:
Upvote 0
Haven't been on here in a while, but just tested the code and an error message popped up saying "type mismatch' with If Date - CDate(Left(sh.Name, 10)) >= 1095 Then highlighted...any suggestions?
 
Upvote 0

Forum statistics

Threads
1,214,523
Messages
6,120,033
Members
448,940
Latest member
mdusw

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