VBA - Rename all sheet names by adding year

ravecake

New Member
Joined
Oct 26, 2022
Messages
13
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
Hi,

I have a workbook of sheets in descending working-day order in "M-D" format as follows: [ 12-30 ] [ 12-29 ] [ 12-28 ] [ 12-23 ] [ 12-22 ] ... [ 7-1 ] etc. This goes back multiple months.

Given the new year, I would like to write a macro that will rename all sheets in "YYYY-M-D" format. In other words, "2022-" will be added to the beginning of each sheet name as follows: [ 2022-12-30 ] [ 2022-12-29 ] [ 2022-12-28 ] ... [ 2022-7-1 ] etc.

Separately, I would also like to know if all sheets can be re-formatted in "YYYY-MM-DD", e.g. [ 2022-7-1 ] → [ 2022-07-01 ].

Would appreciate any assistance!
Thanks.
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Try this:
VBA Code:
Sub Add_Year()
    Dim ws As Worksheet
    For Each ws In ThisWorkbook.Worksheets
        ws.Name = "2022-" & ws.Name
        ws.Name = Format(ws.Name, "YYYY-MM-DD")
    Next ws
End Sub
 
Upvote 0
Try this:
VBA Code:
Sub Add_Year()
    Dim ws As Worksheet
    For Each ws In ThisWorkbook.Worksheets
        ws.Name = "2022-" & ws.Name
        ws.Name = Format(ws.Name, "YYYY-MM-DD")
    Next ws
End Sub

Thanks for the quick response - I needed this as a personal.xlsb macro but the above wouldn't work (nothing would happen when executing). It does however work when I change ThisWorkbook to ActiveWorkbook in the code. Any idea why this is?
 
Upvote 0
Thanks for the quick response - I needed this as a personal.xlsb macro but the above wouldn't work (nothing would happen when executing). It does however work when I change ThisWorkbook to ActiveWorkbook in the code. Any idea why this is?
ThisWorkbook is the workbook that contains the code (which is what I assumed you wanted) whereas ActiveWorkbook is whatever workbook you happen to have active when you run the code.
 
Upvote 0
ThisWorkbook is the workbook that contains the code (which is what I assumed you wanted) whereas ActiveWorkbook is whatever workbook you happen to have active when you run the code.
Yeah this makes sense.

Just while I have you here - if all sheets are named in YYYYMMDD [ 20221230 ] [ 20221229 ] etc how can I rename them all to YYYY-MM-DD?

Conversely, if they are all in YYYY-MM-DD how do I remove the hyphens and reformat to YYYYMMDD?

Thanks!
 
Upvote 0
Just while I have you here - if all sheets are named in YYYYMMDD [ 20221230 ] [ 20221229 ] etc how can I rename them all to YYYY-MM-DD?

Conversely, if they are all in YYYY-MM-DD how do I remove the hyphens and reformat to YYYYMMDD?

To add hyphens where they don't currently exist, use:
VBA Code:
For Each ws In ActiveWorkbook.Worksheets
        ws.Name = Format(ws.Name, "YYYY-MM-DD")
Next ws

To remove the hyphens where they currently exist, use:
VBA Code:
For Each ws In ActiveWorkbook.Worksheets
        ws.Name = Format(ws.Name, "YYYYMMDD")
Next ws
 
Upvote 0
To add hyphens where they don't currently exist, use:
VBA Code:
For Each ws In ActiveWorkbook.Worksheets
        ws.Name = Format(ws.Name, "YYYY-MM-DD")
Next ws

To remove the hyphens where they currently exist, use:
VBA Code:
For Each ws In ActiveWorkbook.Worksheets
        ws.Name = Format(ws.Name, "YYYYMMDD")
Next ws
Ahh perfect, thanks so much! Appreciate your help.
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,187
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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