VBA macros · Sheets
VBA macro to rename sheets from a list in Excel
Rename many sheets at once from a two-column list of old and new names. The macro skips missing sheets and reports invalid names instead of stopping.
Renaming forty sheets by double-clicking each tab is slow and easy to get wrong. A better way is to type the old and new names into two columns, check the list, and let a macro do the renaming. Put the old names in column A and the new names in column B of any sheet, with headings in row 1, make that sheet active and run the macro.
Option Explicit
Sub RenameSheetsFromList()
' Column A = current sheet name, column B = new name, headings in row 1
Dim lst As Worksheet
Dim wb As Workbook
Dim sh As Object
Dim lastRow As Long, r As Long
Dim oldName As String, newName As String
Dim done As Long
Dim problems As String
Set lst = ActiveSheet
Set wb = lst.Parent
lastRow = lst.Cells(lst.Rows.Count, "A").End(xlUp).Row
For r = 2 To lastRow
oldName = Trim$(lst.Cells(r, 1).Text)
newName = Trim$(lst.Cells(r, 2).Text)
If Len(oldName) > 0 And Len(newName) > 0 And oldName <> newName Then
Set sh = Nothing
On Error Resume Next
Set sh = wb.Sheets(oldName)
On Error GoTo 0
If sh Is Nothing Then
problems = problems & vbLf & oldName & ": sheet not found"
Else
On Error Resume Next
sh.Name = newName
If Err.Number <> 0 Then
problems = problems & vbLf & oldName & " -> " & newName & ": " & Err.Description
Else
done = done + 1
End If
On Error GoTo 0
End If
End If
Next r
If Len(problems) > 0 Then
MsgBox done & " sheet(s) renamed." & vbLf & "Not renamed:" & problems, vbExclamation
Else
MsgBox done & " sheet(s) renamed.", vbInformation
End If
End SubHow to use it
- Open the workbook and press
Alt+F11(Mac:Opt+F11orFn+Opt+F11) to open the VBA editor. See The Developer tab and the VBA editor if you have never used it. - Choose Insert › Module. An empty code window opens on the right.
- Paste the whole code into that window. If the module already starts with
Option Explicit, do not paste that line twice. - Go back to Excel, press
Alt+F8(Mac:Opt+F8, or Tools › Macro › Macros), pickRenameSheetsFromListand click Run. - Before running, make the sheet with the list the active sheet.
- Save the file as an Excel Macro-Enabled Workbook (.xlsm). A normal .xlsx file throws the code away when you save it.
To have this macro ready in every workbook, keep it in your Personal Macro Workbook (PERSONAL.XLSB). If you do not have one yet, record any tiny macro with Store macro in: Personal Macro Workbook (see The macro recorder: record, read and clean up code), then paste this code into a module under VBAProject (PERSONAL.XLSB). The macro works on whichever workbook is active when you run it.
How it works
The last filled row in column A is found with End(xlUp), the standard way explained in Find the last row and build dynamic ranges in VBA. Each row is then read with .Text (what the cell shows) and Trim$ removes stray spaces, a common reason why a name "does not match" when it looks identical.
Set sh = wb.Sheets(oldName) fails when no sheet has that name, so it runs under On Error Resume Next; if the lookup failed, sh is still Nothing and the row is added to the problem list. Sheet names are not case-sensitive here, so sales finds the sheet Sales.
Renaming can fail too: names longer than 31 characters, names containing \ / ? * [ ] :, an empty name or a name that another sheet already uses are all refused. Instead of stopping at the first bad row, the macro records Err.Description and carries on, then shows a single summary at the end. That pattern, trying an action and collecting failures, is explained in Error handling and debugging in VBA.
Change it
If you do not need old names, because the sheets are simply in the right order, rename by position instead: the first name in the list goes to sheet 1, the second to sheet 2, and so on.
Sub RenameSheetsByPosition()
' New names in A2 downwards of the active sheet; sheet 1 gets A2, sheet 2 gets A3...
Dim lst As Worksheet
Dim i As Long
Set lst = ActiveSheet
For i = 1 To ActiveWorkbook.Sheets.Count
If Len(Trim$(lst.Cells(i + 1, 1).Text)) = 0 Then Exit For
ActiveWorkbook.Sheets(i).Name = Trim$(lst.Cells(i + 1, 1).Text)
Next i
End SubNote that in this version the list sheet itself is renamed too if it is one of the first sheets. Use VBA macro to list all sheet names in Excel to produce the current names in column A, copy them to column B, edit them, and you have a perfect input list for the main macro.
What can go wrong
Formulas in other sheets update automatically when a sheet is renamed, so ='Jan'!B5 becomes ='January'!B5. Text that only looks like a reference does not: INDIRECT("Jan!B5"), VBA code that uses Sheets("Jan") and hyperlinks created earlier keep the old name and break. Search for them before renaming.
Related: VBA macro to list all sheet names in Excel, VBA macro to create sheets from a list of names in Excel.