VBA Code - Rename Sheets
A VBA macro to rename multiple sheets based on a list of old and new names in an Excel sheet.
Harsh C
February 2, 2025 · 3 min read
See it in action
Why you should use?
Suppose you have a list of 20 sheets and you want to organize it in a proper format. Renaming each sheet manually can be time-consuming and prone to errors. By using a VBA macro, you can automate this task, ensuring consistency and saving valuable time. This method is especially useful for large workbooks with numerous sheets that need to be renamed according to a predefined list. The VBA macro will handle the renaming process in seconds, allowing you to focus on more important tasks.
Here is the instance for your better understanding! Let's say you have a table with sheet names (using the VBA code to list the names refer VBA Code - List All Sheets) and new names that need to be changed. If you do this manually, it might take you around 1-2 minutes (based on the example). However, by using the VBA code provided below, this task will be completed in a fraction of a second.
| Sheet Name | Change Name to |
|---|---|
| Tally | Tally Extract |
| Gross Block Abstract | Summary |
| FAR (Working) | FAR |
| Addition | Addition of Assets |
| Deletion | Deletion of Assets |
| FAR (as provided) | Client Workings |
Impact of VBA Code
Task - Renaming Multiple Sheets
| Particulars | Time Taken to complete |
|---|---|
| Without using VBA Code | 1-2 minutes |
| VBA Code (6 sheets) | 1 seconds |
VBA Code
Below is the VBA macro to rename sheets:
Sub M31_RenameSheets()
Dim ws As Worksheet
Dim nameSheet As Worksheet
Dim oldName As String
Dim newName As String
Dim i As Integer
Dim found As Boolean
' Set the sheet where the old and new names are stored
Set nameSheet = ActiveWorkbook.ActiveSheet ' Change this to your actual sheet name
' Loop through the sheets
For Each ws In ActiveWorkbook.Sheets
' Start from the first row (change as needed)
i = 1
found = False
' Loop through the names in the active sheet
Do While nameSheet.Cells(i, 1).Value <> ""
oldName = nameSheet.Cells(i, 1).Value
newName = nameSheet.Cells(i, 2).Value
' Check if the current sheet name matches the old name
If ws.Name = oldName Then
' Rename the sheet
ws.Name = newName
found = True
Exit Do
End If
i = i + 1
Loop
If Not found Then
Debug.Print "Sheet not found in list: " & ws.Name
End If
Next ws
MsgBox "Sheets renamed successfully!"
End SubUsage Instructions
- Open Excel and press
Alt + F11to open the VBA Editor. - Insert a new module (
Insert > Module). - Copy and paste the above VBA code.
- Create a sheet with a list of old and new sheet names in Column A and Column B.
- Run
M31_RenameSheetsmacro. - Your sheets will be renamed automatically!