I have to create weekly reports and send to different clients. I have a macro to copy required data for each client and give new sheet a name mmm-dd-yyyy (ex. Mar-01-2021).
I only want to keep last four weeks of sheets. I found code to delete any sheet older than one month but it does not work.
I have hidden sheets (Master and contact) which should stay as is. I might add Leadtime sheet in the future which will be visible to clients that should not be deleted.
Sub del_by_date2()
Dim tagad As Date
Dim pirms1 As Date
tagad = Now()
pirms1 = DateAdd("m", -1, tagad)
test = Format(pirms1, "mmm-dd-yyyy")
Application.DisplayAlerts = False
For Each Worksheet In ThisWorkbook.Sheets
If Right(Worksheet.Name, 4) < Right(test, 4) Then Worksheet.Delete
ElseIf Right(Worksheet.Name, 4) = Right(test, 4) _ And Left(Worksheet.Name, 2) <= Left(test, 2)
Then
Worksheet.Delete
End If
Next
Application.DisplayAlerts = True
End Sub