To get a worksheet name in VBA, use the Name property of a Worksheet object: MsgBox ActiveSheet.Name returns the name of the sheet the user is currently on, and MsgBox Worksheets("Sheet1").Name returns the name of a specific sheet. ThisWorkbook.Worksheets(1).Name grabs the first sheet in the workbook. Every sheet also has a hidden CodeName property you can read in the same way, and that value is what you actually use to refer to a sheet safely when its tab may be renamed. None of this needs a reference or an add-in, only a macro-enabled workbook and access to the VBA editor.

What You'll Need

  • Excel for Windows or Mac with a workbook saved as .xlsm (macro-enabled), or a temporary workbook you intend to test in.
  • Permission to run macros. Corporate environments sometimes block them through Trust Center settings or group policy, in which case talk to whoever manages your machine rather than trying to work around it.
  • Access to the VBA editor, opened with Alt+F11 on Windows or Option+F11 on Mac.

Step-by-Step

  1. Open the VBA editor. Press Alt+F11 (Windows) or Option+F11 (Mac). If nothing happens, your keyboard or system may intercept the shortcut; use the Developer tab, then Visual Basic, instead. If you cannot see a Developer tab, enable it in Excel's options under the customize-the-ribbon section. Menu labels and tab names can differ slightly by version and region.
  2. Insert a module. In the editor, choose Insert > Module. This gives you a blank code window. Do not paste sheet-reading code into a sheet's own code module by accident, because there it behaves differently and is harder to test. Use a standard module for utility macros.
  3. Write the simplest version first. Type this and press F5:
    Sub ShowActiveSheetName()
        MsgBox ActiveSheet.Name
    End Sub
    ActiveSheet is whatever sheet happens to be on top at the moment the code runs. Switch to another tab, run it again, and confirm the message changes. This one line is the answer to most requests about getting a worksheet name.
  4. Read a specific sheet by index. Index numbers start at 1 and follow the order of the tabs, not the order of the names:
    Sub FirstSheetName()
        MsgBox ThisWorkbook.Worksheets(1).Name
    End Sub
    ThisWorkbook means the file containing the code. Use ActiveWorkbook if you deliberately want the workbook in front, which may be a different file. Mixing the two is the single most common cause of a macro acting on the wrong workbook.
  5. Read a specific sheet by its tab name. Pass the name as a string:
    Sub NamedSheetName()
        MsgBox Worksheets("Sales").Name
    End Sub
    Odd as it looks, this is useful when the name is built dynamically, for example from a text cell, or when you are checking that a sheet you expect really is there.
  6. Loop through every sheet. To list all tab names in one go:
    Sub ListSheetNames()
        Dim ws As Worksheet
        Dim msg As String
        For Each ws In ThisWorkbook.Worksheets
            msg = msg & ws.Name & vbCrLf
        Next ws
        MsgBox msg
    End Sub
    Worksheets covers ordinary sheets only. Use Sheets if the workbook also contains chart sheets and you want those included too.
  7. Return the name to the rest of your code, not just a message box. Message boxes are for testing. In real use, put the name in a variable or a cell:
    Sub WriteNameToCell()
        Dim sheetName As String
        sheetName = ActiveSheet.Name
        Range("A1").Value = sheetName
    End Sub
    Declare with Dim sheetName As String rather than relying on a Variant, so mistakes surface during testing rather than in production.
  8. Get the sheet name from the worksheet's own perspective. This code lives inside a sheet module rather than a standard module:
    MsgBox Me.Name
    Me refers to the sheet holding the code, so it always reports that sheet no matter which tab is active. This is handy for event code that fires when the sheet is edited.
  9. Know the difference between Name and CodeName. Every sheet has both. Name is the visible tab label, which a user can rename at any time. CodeName is the internal identifier, usually something like Sheet1, which stays put unless the project is edited deliberately. Read it with ThisWorkbook.Worksheets(1).CodeName. For macros that must keep working after someone renames a tab, refer to the sheet by CodeName and read Name only when you need to display or compare visible text.
  10. Test the failure cases before you rely on it. Temporarily run your macro with the wrong workbook active, with a name that does not exist, and from a sheet that is protected. Note which errors appear, then add handling. A macro that only works when everything is perfect will break the first time a colleague uses it.

Common Mistakes

  • Assuming ActiveSheet means the sheet you clicked. If your code opens workbooks, activates ranges or shows dialogs first, the active sheet can change underneath you. Capture the name at the start of the routine into a string variable.
  • Confusing Worksheets with Sheets. Worksheets skips chart sheets and dialog sheets, so index 1 may not be the leftmost tab if a chart sheet sits in front of it.
  • Hard-coding tab names. Worksheets("Sheet1") fails with a subscript out of range error the moment someone renames the tab. CodeName survives renames.
  • Using the generic ActiveWorkbook when the code lives in a specific file. Tests pass, then the macro edits the wrong workbook once a second file is open.
  • Forgetting that names must be unique. Excel will not allow two sheets with the same name in one workbook, but names can differ only in case or trailing spaces in nearby workbooks, so comparisons should be done carefully.

Tips

  • Store the name once at the top of a routine and reuse the variable. Re-reading ActiveSheet.Name mid-routine invites subtle bugs.
  • Trim text taken from a cell before matching it to a sheet name: Trim$(Range("A1").Value). Invisible trailing spaces cause more mismatches than anything else.
  • Use Debug.Print ws.Name instead of message boxes when looping over many sheets; output appears in the Immediate window and does not interrupt the run.
  • If you need the workbook name as well, pair it with ThisWorkbook.Name or ThisWorkbook.FullName for the path.
  • Name sheets with no leading or trailing spaces and avoid punctuation that clashes with formula syntax, so later string work stays simple.

Troubleshooting

The macro does not run at all. Check that the file is saved as macro-enabled, that macros are allowed in Trust Center, and that the code sits in a standard module. If the editor opens but F5 is greyed out, click inside the subroutine first.

Run-time error 9, subscript out of range. The sheet name or index you asked for does not exist in that workbook. Print the names with the loop from step 6 to see the real spellings, including case and spaces.

Run-time error 424, object required. You are treating something that is not an object as one, often because a variable was never assigned a sheet. Check the variable declaration and the line that should set it.

The wrong sheet name appears. Almost always this is ActiveSheet versus ThisWorkbook. Confirm which workbook is in front and whether your routine activates anything before reading the name.

Nothing happens with no error. Event code may be disabled, or the macro may be sitting in a sheet module that never fires. Run it manually from the editor to isolate the cause. Before deleting or resetting anything, remember that removing a module loses the code in it, and saving over a workbook after a failed run can lose data, so keep a backup copy first.

Frequently Asked Questions

How do I get the name of the active sheet in one line?

MsgBox ActiveSheet.Name is the shortest working version. Assign it to a string if you need to use it later.

Can I get a sheet name by its index number?

Yes. ThisWorkbook.Worksheets(3).Name returns the third sheet in tab order. The index reflects position, so inserting or moving tabs changes it.

Why does my macro break after someone renames a tab?

Because it refers to the sheet by its visible name. Switch to the CodeName property, which does not change when the tab is renamed.

Does the active sheet name include the workbook name?

No. Name is only the tab label. Add ThisWorkbook.Name if you need the file name too.

Can I read sheet names from a closed workbook?

Not with the plain Name property. You would have to open the file, read the names, and close it again, which is more complex and should be done carefully.

Do chart sheets behave the same way?

Chart sheets also expose a Name property, but they are not members of the Worksheets collection. Use the Sheets collection to reach both types.