Programmatically renaming sheets with variables

tourless

Board Regular
Joined
Feb 8, 2007
Messages
144
Office Version
  1. 365
Platform
  1. Windows
Hi Folks,

I'm looking for a way to rename all the sheets in a workbook. We have many workbooks that contain a worksheet for each week of the year. They are all nemd in the following convention... "WE 01-07-23", "WE 01-14-23", etc. When preparing new workbooks for the coming year, we make copies of the current workbooks and have to rename all the sheets manually. Is it possible to rename them all with a bit of code? I've looked around the forums and have found posts that speak to renaming sheets but only by taking a value from a given cell or cells, but nothing that looks to my problem.

My thought is to name the first sheet appropriately then programmatically rename the remaining sheets. My problem is I don't know enough about Excel to know where to find and access an existing worksheets Name value, place it in a variable and manipulate it from there. Can anyone help point me in the right direction? -thanks.
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Are there any sheets in your workbook before the first Week-Ending sheet (i.e. "WE 01-07-23")?

Will all the sheets needed for the year already be in the workbook at the time you want this code to run, so that we just need to rename all the existing sheets?
 
Upvote 0
Are there any sheets in your workbook before the first Week-Ending sheet (i.e. "WE 01-07-23")?

Will all the sheets needed for the year already be in the workbook at the time you want this code to run, so that we just need to rename all the existing sheets?
1. No there are not. The first sheet in the workbook is always the week ending date of the first week of the year (week ending on Saturday).
2. Yes. All 52 sheets already exist in the workbooks. There is no need to count and/or create new sheets.
 
Upvote 0
Excellent. I think the following code should do what you want:
VBA Code:
Sub MyNameSheets()

    Dim n1 As String
    Dim d1 As Date
    Dim i As Long
    Dim n2 As String
    
'   Get date from first sheet name
    n1 = Mid(Sheets(1).Name, 4)
    d1 = DateSerial(Right(n1, 2), Left(n1, 2), Mid(n1, 4, 2))
    
'   Loop through all sheets
    If Sheets.Count > 1 Then
        For i = 2 To Sheets.Count
'           Rename sheet
            n2 = "WE " & Format(d1 + ((i - 1) * 7), "mm-dd-yy")
            Sheets(i).Name = n2
        Next i
    End If
    
    MsgBox "Macro complete!"
    
End Sub
 
Upvote 0
Solution
You are welcome.
Glad I was able to help!
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,906
Members
452,366
Latest member
TePunaBloke

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