Smart Systems for Home

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

Excel VBA Name Property: A Practical How-To

· 6 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

The Name property in Excel VBA lets you read or change the name of a worksheet, chart sheet, or workbook from code. You can rename a sheet with Worksheets("Sheet1").Name = "Sales", or read the current name with MsgBox ActiveSheet.Name. The property is read/write for worksheets and workbooks, but you cannot set the Name property of a workbook to a name that is already open, and worksheet names must be unique within their workbook. This guide walks through the practical steps, the errors you will hit, and how to avoid breaking formulas that reference the old name.

What You'll Need

  • Excel on Windows or Mac with the Developer tab enabled (File > Options > Customize Ribbon > check Developer, or Excel > Preferences > Ribbon & Toolbar on Mac).
  • A workbook saved as .xlsm if you want to keep your macro code.
  • Basic familiarity with opening the VBA Editor (Alt+F11 on Windows, Option+F11 on Mac) and inserting a module (Insert > Module).
  • Permission to edit the workbook — if it is protected or shared, renaming will fail until you unprotect it.

Step-by-Step

  1. Open the VBA Editor. Press Alt+F11 (Windows) or Option+F11 (Mac). The editor opens in a separate window. Menu labels can differ slightly by Excel version and platform.
  2. Insert a standard module. In the VBA Editor, choose Insert > Module. A blank code pane appears. Keeping code in a module (rather than a sheet module) makes it easier to reuse.
  3. Read a sheet name first. Type this procedure and press F5 to run it:
    Sub ReadName()
        MsgBox ActiveSheet.Name
    End Sub
    This confirms which sheet is active and shows the exact string, including any trailing spaces, that you will need to match.
  4. Rename a worksheet by its current name. Use the Worksheets collection and assign a new string:
    Sub RenameSheet()
        Worksheets("Sheet1").Name = "Sales2024"
    End Sub
    Run it with F5. The tab label changes immediately. If the name already exists in the workbook, Excel raises run-time error 1004.
  5. Rename the active sheet without hard-coding. This avoids errors when the tab has already been renamed:
    Sub RenameActive()
        ActiveSheet.Name = "Summary"
    End Sub
    Use this when the user has selected the sheet they want renamed.
  6. Loop through all sheets and rename by pattern. The following adds a prefix to every worksheet while skipping chart sheets:
    Sub PrefixSheets()
        Dim ws As Worksheet
        For Each ws In ThisWorkbook.Worksheets
            ws.Name = "Dept_" & ws.Name
        Next ws
    End Sub
    Note that ThisWorkbook.Worksheets excludes chart sheets; use Sheets if you need both types.
  7. Rename a workbook. The Name property of a Workbook object is read-only in the sense that you cannot assign a new file name through it — assigning raises an error. To rename the file, use SaveAs with a new file name:
    Sub RenameWorkbookFile()
        ThisWorkbook.SaveAs ThisWorkbook.Path & "\NewName.xlsm"
    End Sub
    This creates a new file; the old one remains on disk unless you delete it.
  8. Check for name conflicts before assigning. Wrap the assignment in a helper that tests whether the target name is free:
    Function SheetNameExists(n As String) As Boolean
        Dim ws As Worksheet
        On Error Resume Next
        Set ws = ThisWorkbook.Worksheets(n)
        SheetNameExists = Not ws Is Nothing
        On Error GoTo 0
    End Function
    Then call it before renaming and show a message if the name is taken.
  9. Handle invalid characters. Excel rejects names containing : \ / ? * [ ] and names longer than 31 characters. Validate with a simple check before assigning:
    If Len(newName) > 31 Or newName Like "*[:\/?*\[\]]*" Then
        MsgBox "Invalid sheet name"
        Exit Sub
    End If
  10. Save and test. Press Ctrl+S. Run the macro on a copy of your workbook first, because renaming a sheet updates formulas that reference it by name, and any external links pointing to the old name will break.

Common Mistakes

  • Assuming Workbook.Name is writable. It is not. Use SaveAs to change the file name.
  • Forgetting the 31-character limit. Excel truncates or errors depending on how you assign; validate first.
  • Using Sheets when you mean Worksheets. Sheets includes chart sheets, which behave differently and can cause type mismatch errors when declared as Worksheet.
  • Renaming a sheet that formulas reference. Excel updates internal references automatically, but external workbook links and VBA code that hard-codes the old name will break silently.
  • Ignoring protection. A protected workbook or sheet blocks renaming; unprotect first, then re-protect after the change.
  • Trailing spaces. A name like "Sales " looks identical on the tab but will not match Worksheets("Sales"). Use the read step to confirm exact strings.

Tips

  • Store new names in a table on a hidden sheet and loop through them — this keeps renaming consistent across many workbooks.
  • Use ActiveSheet.Name inside error messages so users see exactly which sheet failed.
  • After renaming, refresh any pivot tables or charts that reference the sheet by name; most update automatically, but external data connections may not.
  • Keep a log sheet that records old and new names so you can trace broken links later.
  • On Mac, keyboard shortcuts differ (Option+F11 for the editor), but the object model and property behaviour are the same.

Troubleshooting

Run-time error 1004 “That name is already taken”. Another sheet in the same workbook uses that name. Check with the helper function above, or loop through Worksheets and compare names case-insensitively.

Run-time error 9 “Subscript out of range”. The sheet name you passed to Worksheets() does not exist. Confirm the exact spelling, including spaces, using the read step.

Run-time error 1004 “Cannot rename a sheet”. The workbook or sheet is protected, or the workbook is shared. Unprotect via Review > Unprotect Sheet, or unshare via Review > Share Workbook (legacy) before renaming.

The macro runs but nothing changes. You may be renaming a sheet in a different workbook than the one you are viewing. Qualify with ThisWorkbook or ActiveWorkbook explicitly.

Formulas show #REF! after renaming. Undo with Ctrl+Z immediately, then check for external links or VBA code that hard-codes the old name and update them before retrying.

Frequently Asked Questions

Can I rename a workbook with the Name property?

No. Workbook.Name is read-only. Use SaveAs with a new file name, which creates a new file and leaves the original in place.

Why does assigning a name raise error 1004?

The most common causes are a duplicate name in the same workbook, a name longer than 31 characters, invalid characters (: \ / ? * [ ]), or a protected/shared workbook.

Does renaming a sheet break formulas?

Formulas inside the same workbook update automatically. External links, hard-coded VBA strings, and some data connections do not, so review them after renaming.

Can I rename chart sheets the same way?

Yes. Chart sheets also have a Name property. Use Sheets("Chart1").Name = "Revenue" or loop through Sheets rather than Worksheets.

Is the Name property case-sensitive?

Excel treats sheet names case-insensitively for uniqueness, so "Sales" and "sales" cannot coexist. Comparisons in VBA should use StrComp with vbTextCompare if you need case-insensitive matching.

What happens if I rename the active sheet while a macro is running?

The object reference remains valid; only the name string changes. Code that later refers to the sheet by its old name will fail, so store a reference variable (Set ws = ActiveSheet) and use it throughout.