Forum Discussion

James Niday's avatar
James Niday
Copper Contributor
Aug 31, 2018

Naming worksheets with successive Sunday dates

I use workbooks composed of worksheets for each week.  Is there an easy way to label the worksheets with successive Sunday dates?

1 Reply

  • Haytham Amairah's avatar
    Haytham Amairah
    Silver Contributor

    Hi James,

     

    In such a repetitive task the macro is the best solution!

     

    So I suggest to use this macro:

    Sub NamingWithSuccessiveSundayDates()
        'Naming worksheets with successive Sunday dates
        'Written by Haytham Amairah
        'Created: 9/1/2018
        'Last update: 9/1/2018
        
        On Error GoTo Handler
        Application.ScreenUpdating = False
        
        Dim startDate As Date
        Dim nextDate As Date
        
        For Each Sheet In Sheets
            startDate = DateValue(Sheet.Name)
            nextDate = DateAdd("ww", 1, startDate)
            
            If Sheet.Index = Sheets.Count Then
                Exit For
            Else
                Sheets(Sheet.Index + 1).Name = Month(nextDate) & "-" & Day(nextDate) & "-" & Year(nextDate)
            End If
        Next
        
        Application.ScreenUpdating = True
        
        Exit Sub
        
    Handler:
        MsgBox ("The name of the worksheet isn't valid!")
        Application.ScreenUpdating = True

    End Sub

     

    But before you run this macro, you have to rename the first worksheet with the starting Sunday date as shown in the below screenshot:

     

    Since slashes aren't allowed in worksheet names, I've used dashes instead!

    So the dates format will look like this: (m-d-yyyy).

     

    Please note that this macro isn't restricted on Sundays only, just rename the first worksheet with any date in any weekday you want.

     

    Hope that helps

Resources