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.
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 Sub
Usage Instructions
- Open Excel and press
Alt + F11
to 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_RenameSheets
macro. - Your sheets will be renamed automatically!