You rename a worksheet in VBA by setting the worksheet's Name property, for example Worksheets("Sheet1").Name = "Sales". You can also use a variable for the old or new name, loop through every sheet and rename each one, or target the active sheet with ActiveSheet.Name. That single line replaces the need to right-click a tab and type a new name, which matters when you have dozens of sheets or need renaming to happen as part of a larger macro.
One thing to know before you start: Excel treats sheet names as unique, and a rename fails if the new name is already used, contains forbidden characters, is longer than 31 characters, or is empty. This guide keeps every step to those known rules. Note that menu labels and dialog wording can differ slightly by Excel version, device and region.
What You'll Need
- Excel on Windows or Mac with the Visual Basic Editor available (press Alt+F11 on Windows, or use the Developer tab on Mac).
- A workbook saved in an enabled macro format (
.xlsm) before you run anything you want to keep. - If the Developer tab is hidden, enable it under File > Options > Customize Ribbon and tick Developer in the right-hand list (on Mac, look under Excel > Preferences > Ribbon & Toolbar).
- Macro security set to a level that lets you run your own macros; do not lower security for files from other people.
Step-by-Step
- Save a copy of the workbook first. Renaming sheets cannot be undone with Ctrl+Z once the macro runs, so File > Save As to a backup name is cheap insurance.
- Open the Visual Basic Editor. On Windows press Alt+F11. On Mac use Tools > Macro > Visual Basic Editor (or the Developer tab if it is shown).
- Insert a standard module: Insert > Module. The module is where your renaming code lives. Do not paste code into the ThisWorkbook or a sheet module unless you specifically want it to fire on an event such as opening the file.
- Type a short macro. The simplest working version is:
Sub RenameSheet()Worksheets("Sheet1").Name = "Sales"End Sub
ReplaceSheet1with the current tab name andSaleswith the name you want. Use the sheet'sCodeNameonly if you understand the difference; the visible tab name is whatWorksheets("…")matches. - Run the macro with F5 while the cursor is inside the sub, or go back to Excel and use Developer > Macros, select RenameSheet and click Run.
- Check the tab. The new name appears immediately. If Excel shows a runtime error, note the message: it tells you whether the name is a duplicate, too long, or invalid — the three usual culprits.
- To rename the currently selected sheet instead of a fixed one, use
ActiveSheet.Name = "NewName". That is handy when the user clicks a tab and then triggers the macro from a button. - To rename using a value typed at run time, add an input line:
Dim newName As String, thennewName = InputBox("New sheet name"), thenActiveSheet.Name = newName. Adding a check such asIf Len(newName) = 0 Then Exit Subprevents the empty-name error. - To rename every sheet in order, loop:
Dim ws As Worksheet,Dim i As Integer,i = 1,For Each ws In ThisWorkbook.Worksheets, then inside the loopws.Name = "Data" & iandi = i + 1,Next ws. The counter keeps names unique automatically. - To rename a sheet and also change the tab colour in one go, add
Worksheets("Sales").Tab.Color = vbGreenafter the rename line. Colour is a property of the sheet object, so the rename and the colour are independent. - Save the workbook as
.xlsmwith File > Save As and choose the macro-enabled format. A normal.xlsxsave strips the code.
Common Mistakes
Using a name that already exists. Sheet names must be unique across the workbook, including hidden sheets. Renaming "Jan" to "Feb" when another "Feb" exists fails. Check hidden sheets under the tab right-click menu before assuming the name is free.
Forbidden characters. A sheet name cannot contain : \ / ? * [ or ]. Referencing those in code produces an error rather than a rejection message you can read easily.
Length limits. The name must be 1 to 31 characters. Trimming long text from cells is a common need; use Left(text, 31) to avoid the error.
Referencing formulas and charts. If formulas elsewhere point at the old name, they update automatically when the sheet name changes. But external links, PivotTables based on that sheet, and hard-coded strings in other macros do not, so search for the old name after renaming.
Running code in the wrong module. Event code placed in ThisWorkbook runs on open, save and other events and can produce unexpected loops. Keep plain renaming subs in a standard module.
Forgetting Mac differences. Keyboard shortcuts differ, and some older Mac versions have limited VBA support. The property syntax above is the same, but verify the macro runs in your version before relying on it.
Tips
Use On Error Resume Next only around a rename you know may fail, and pair it with Err.Number checks. Wrapping an entire macro silences real problems.
Store the old name first if you might need to undo: Dim oldName As String: oldName = ws.Name, then rename. A second macro can restore it.
Build new names from a pattern such as a month list or cell values, and pad numbers with Format(i, "00") so sheets sort correctly.
To see every current sheet name before bulk renaming, loop and Debug.Print ws.Name, then check the Immediate window (Ctrl+G).
Keep a single renaming sub, not several near-copies. Fewer versions mean fewer places to fix when a name collides.
Troubleshooting
Compile error before anything runs. Usually a missing quote, missing Dim for a variable, or code pasted into the wrong place. Compare your lines with the examples exactly.
Run-time error 1004. The name is invalid — duplicate, empty, too long, or contains a forbidden character. Try the intended name manually on the tab; if Excel refuses it there, the macro will too.
Nothing happens when you press F5. The cursor must sit inside the sub. Use Developer > Macros and pick the macro by name instead.
The rename works but other things break. Formulas, chart series or PivotTable sources may still reference the old name. Update them or refresh the PivotTable. Check any other macro that hard-codes the old name.
Code disappears after saving. You saved as .xlsx. Reopen the backup, Save As .xlsm, and re-add the macro if needed.
Macro blocked entirely. Check File > Options > Trust Center > Trust Center Settings > Macro Settings, or the banner warning, and only enable macros in a file you created or trust.
Frequently Asked Questions
Can I rename a sheet to a name already used elsewhere?
No. Worksheet names must be unique in a workbook, so renaming to an existing name fails with a run-time error. Delete or rename the other sheet first.
Does renaming a sheet break formulas that reference it?
Internal formulas that point at the old name update automatically. External links and hard-coded strings in other macros do not, so check those separately.
Can I rename a hidden sheet?
Yes, the Name property works on hidden sheets. You cannot select a hidden sheet interactively until you unhide it, but code can rename it directly.
How long can a sheet name be?
From 1 to 31 characters. Names are also case-insensitive for uniqueness purposes, so "Sales" and "sales" count as the same name.
Can I undo a VBA rename?
Not with Ctrl+Z. Store the old name in a variable or a backup copy, or use File history if the file was saved before the change.