Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA: Rename a Worksheet with Code

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To rename a worksheet in Excel with VBA, set the Name property of the Worksheet object to a new string, for example Worksheets("Sheet1").Name = "Summary". Run that line from the Visual Basic Editor (Alt+F11) or a macro, and the tab label changes immediately. The name must be unique, 31 characters or fewer, and cannot contain the characters : \ / ? * [ ].

What You'll Need

  • Excel for Windows or Mac with macros enabled. The steps below use the Windows ribbon; Mac menus are similar but labels can differ slightly by version, device or region.
  • A workbook saved as .xlsm (macro-enabled) if you want the code to persist. Standard .xlsx files drop macros on save.
  • The Developer tab visible, or the ability to press Alt+F11. To show the Developer tab, go to File > Options > Customize Ribbon and tick Developer in the right-hand list.
  • Basic awareness of the sheet names in your workbook. Renaming a sheet that does not exist, or to a name already in use, raises a runtime error.

Step-by-Step

  1. Open the workbook you want to change and press Alt+F11 (Windows) or Fn+Alt+F11 on some Mac keyboards to open the Visual Basic Editor.
  2. In the Editor, use Insert > Module to add a blank module. This is where you will paste the code. You can also run a one-off line from the Immediate Window (View > Immediate Window, or Ctrl+G).
  3. Type or paste the rename statement. The simplest form is Worksheets("Sheet1").Name = "Summary". Replace Sheet1 with the current tab name and Summary with the new name.
  4. To rename the active sheet without knowing its name, use ActiveSheet.Name = "Summary". To rename by position, use Worksheets(1).Name = "Summary".
  5. Press F5 to run the macro, or place the cursor on the line and press F8 to step through it. If you are in the Immediate Window, press Enter after typing the line.
  6. Switch back to Excel (Alt+F11 again or click the Excel icon). The tab should now show the new name. Save the workbook if you want the change kept.
  7. For a reusable routine, wrap the line in a Sub. For example: Sub RenameSheet() on one line, the rename line indented, and End Sub on the next. Then run it from Developer > Macros or assign it to a button.
  8. If you need to rename several sheets, repeat the statement with each sheet reference. Keep each name unique and within the 31-character limit.
  9. To guard against errors, add a check before renaming. For example, test whether the name already exists by looping through Worksheets and comparing ws.Name to the proposed string, then only assign if no match is found.
  10. If the new name contains spaces or special characters, keep the quotes around it in the code. VBA treats the text inside quotes literally.

Common Mistakes

  • Using a name that already exists in the workbook. Excel requires unique sheet names, so the code stops with a runtime error 1004.
  • Exceeding 31 characters. The assignment fails and Excel shows an error dialog.
  • Including forbidden characters: colon, backslash, forward slash, question mark, asterisk, square brackets. These are not allowed in sheet names.
  • Leaving the sheet name blank. An empty string is rejected.
  • Referencing a sheet by a name that has already been changed earlier in the same macro. Once renamed, the old name no longer resolves.
  • Forgetting that renaming a sheet does not update formulas that refer to it by name in other workbooks, and it can break external links. Check dependent formulas after the change.
  • Saving as .xlsx and losing the macro. Use .xlsm if the code needs to stay.

Tips

  • Use a variable for the new name so you can change it in one place: Dim newName As String then newName = "Summary" and Worksheets(1).Name = newName.
  • Prefix names with a number or category (for example 01_Input, 02_Report) to keep tabs in a predictable order.
  • If you rename sheets often, store the target names in a small table on a hidden sheet and loop through it, matching by current name.
  • Test on a copy of the workbook first. Renaming is easy to reverse manually, but a macro that renames the wrong sheet can confuse formulas and links.
  • Use ThisWorkbook.Worksheets instead of Worksheets when you want to be sure the code targets the workbook containing the macro, not whichever workbook is active.

Troubleshooting

If the macro stops with a runtime error, read the message. Error 1004 usually means the name is invalid, already taken, too long, or contains a forbidden character. Check the string against those rules first.

If nothing appears to change, confirm you ran the correct line and that the workbook you are looking at is the one the code targeted. When using ActiveSheet, make sure the intended tab was active when the macro ran.

If the code cannot find the sheet, the name in the code may not match the tab exactly, including spaces. Sheet names are case-insensitive in VBA lookups, but spelling and spacing must match.

If the rename works but formulas break, check formulas that reference the old sheet name. Excel usually updates internal references automatically when you rename through the interface, but code-driven renames can leave external links and some formulas pointing at the old name. Review and correct them.

If the macro is missing after reopening, the file was saved without macros. Save as .xlsm and enable macros when prompted. If macros are blocked by policy, the rename line will not run; contact whoever manages the Excel settings.

Frequently Asked Questions

Can I rename a sheet without VBA?

Yes. Right-click the tab, choose Rename, type the new name and press Enter. VBA is only needed when you want to automate the change or repeat it across many sheets.

Why does VBA say the name is invalid?

The name is probably longer than 31 characters, empty, already used in the workbook, or contains one of the forbidden characters : \ / ? * [ ].

Does renaming a sheet with VBA update formulas?

Internal references in the same workbook are generally adjusted, but external links and some formulas may not update. Check dependent formulas after renaming, especially if other workbooks point to the sheet.

Can I rename a hidden sheet?

Yes. The Name property works on hidden sheets. You do not need to unhide the sheet first.

What happens if I use a name that already exists?

The assignment fails with a runtime error. Test for an existing name before renaming, or choose a unique name.

Is there a limit to how many times I can rename a sheet?

No practical limit, but each rename must produce a valid, unique name. Repeated automated renames should include checks to avoid errors.