Copy existing tab and create a new tab with the next date stopped working

50 views Asked by At

Ive always used this code behind my macro to copy the existing tab and then rename it to the next date in the tabs sequence. Only its now stopped working and each time it runs it creates a new tab called 'Sun 1 Jan' Can anyone please help get this working again? thanks


Public Sub SheetAndRenamePredefined()
Dim dMaxDate As Date, wsForLoop As Worksheet, sTestString As String

dMaxDate = DateSerial(1900, 1, 0) 'Default to 0

For Each wsForLoop In ThisWorkbook.Worksheets 'Loop through every worksheet
    sTestString = wsForLoop.Name 'Get the name of the sheet
    If InStr(sTestString, " ") > 0 Then sTestString = Mid(sTestString, 1 + InStr(sTestString, " ")) 'Remove the Weekday

    'If you need to add the Month/Year from your filename, do that here

    If IsDate(sTestString) Then 'Only check Worksheets with Dates for Names
        If CDate(sTestString) > dMaxDate Then dMaxDate = CDate(sTestString) 'If this is a later date then store it
    End If
Next wsForLoop 'Return to start of loop

If dMaxDate < DateSerial(1900, 1, 1) Then dMaxDate = Now - 1 'If we have no Worksheets with Dates for Names then default to yesterday

ActiveSheet.Copy After:=Worksheets(Sheets.Count)
    On Error Resume Next
     ActiveSheet.Name = Format(dMaxDate + 1, "ddd d mmm") 'And Name it the day after our stored date

     Range("F1").Value = Format(dMaxDate + 1, "ddd d mmm")

End Sub
1

There are 1 answers

1
KopkaCobana On

The macro was only clever enough to have one calendar year in the mix. Meaning as soon as I deleted (hiding 2021 date tabs didnt give the same fix) and ran the macro it worked as expected.