To get the active worksheet name in Excel VBA, use the ActiveSheet.Name property. This returns the name of the currently selected sheet as a string, which you can assign to a variable, display in a message box, or use in further code. No additional setup is needed beyond having a workbook open in Excel.
What You'll Need
- Microsoft Excel (any recent version that supports VBA, such as Excel 2010 or later)
- A workbook with at least one worksheet
- Access to the VBA editor (press
ALT + F11to open it)
Step-by-Step
- Open your Excel workbook and ensure the worksheet whose name you want to retrieve is the active sheet (click its tab at the bottom).
- Press
ALT + F11to open the VBA editor. - In the VBA editor, go to the
Insertmenu and selectModuleto create a new module if you don’t already have one. - In the code window, type the following subroutine:
Sub GetActiveSheetName() Dim sheetName As String sheetName = ActiveSheet.Name MsgBox "The active worksheet is named: " & sheetName End Sub - Place your cursor anywhere inside the code and press
F5to run the subroutine, or close the VBA editor and run it viaDeveloper > Macrosin Excel, selectGetActiveSheetName, and clickRun. - A message box will appear displaying the name of the active worksheet.
- Alternatively, to use the name in further code without showing a message box, you can assign
ActiveSheet.Namedirectly to a variable or use it in a formula, such as:If ActiveSheet.Name = "Data" Then ' Perform specific actions End If
Common Mistakes
- Confusing
ActiveSheetwithThisWorkbook.ActiveSheet— while they are equivalent in most cases,ActiveSheetrefers to the sheet active in the entire Excel application, which could be in a different workbook if multiple are open. To avoid ambiguity, especially when working with multiple workbooks, explicitly qualify it asThisWorkbook.ActiveSheet.Nameif you intend to reference the active sheet in the workbook containing the code. - Assuming the active sheet is the same as the sheet where the code is stored. Code in a module runs in the context of the application, so
ActiveSheetdepends on user interaction, not the location of the code. - Using
ActiveSheet.Namewithout checking if a sheet is actually active. Although rare, if no sheet is active (e.g., during initialization), this could cause an error. Always ensure a workbook is open and a sheet is selected before running the code. - Trying to use
ActiveSheet.Namein a worksheet function (like a formula in a cell). VBA properties likeActiveSheet.Namecannot be called directly from Excel formulas; they must be used in macros or VBA functions called viaApplication.Calleror similar mechanisms.
Tips
- Use
ThisWorkbook.ActiveSheet.Nameinstead ofActiveSheet.Namewhen your code might run while another workbook is active, to ensure you’re referencing the sheet in the workbook that contains the macro. - Store the active sheet name in a variable at the start of a macro if you plan to switch sheets during execution and need to return to it later:
Dim originalSheet As String originalSheet = ActiveSheet.Name ' ... code that activates other sheets ... ThisWorkbook.Sheets(originalSheet).Activate
On Error Resume Next
Dim sheetName As String
sheetName = ActiveSheet.Name
If sheetName = "" Then
MsgBox "No active worksheet found."
Else
MsgBox "Active sheet: " & sheetName
End If
On Error GoTo 0
Debug.Print ActiveSheet.Name to send it to the Immediate Window in the VBA editor (press CTRL + G to view it).Troubleshooting
- Error 91: Object variable or With block variable not set: This usually means
ActiveSheetisNothing, which can happen if no workbook is open or if all windows are minimized. Ensure a workbook is open and you’ve clicked on a worksheet tab before running the code. - Error 1004: Application-defined or object-defined error: May occur if the workbook is protected or if Excel is in a modal state (e.g., a dialog box is open). Close any open dialogs and ensure editing is enabled.
- The message box shows an unexpected name (e.g., "Sheet1" when you expected "Data"): Confirm that the correct sheet is active by clicking its tab. The active sheet is the one with the highlighted tab, not necessarily the one you last edited.
- Nothing happens when you run the macro: Check that macro security settings allow macros to run. Go to
File > Options > Trust Center > Trust Center Settings > Macro Settingsand enable macros (note: only do this for trusted files). - You’re using
ActiveSheet.Namein a formula and getting a #NAME? error: As noted, this property cannot be used directly in Excel formulas. Instead, create a VBA function that returns the sheet name and call that function from the cell.
Frequently Asked Questions
Can I get the name of the active worksheet without using VBA?
Not directly in a standard Excel formula. While you can use CELL("filename") to get the full path and workbook name, extracting the active sheet name requires VBA or a named range trick involving macro-enabled workbook functions.
Does ActiveSheet.Name change if I rename the sheet?
Yes. The Name property always reflects the current name of the worksheet. If you rename the sheet via the tab or programmatically, subsequent calls to ActiveSheet.Name will return the new name.
What’s the difference between ActiveSheet.Name and ThisWorkbook.ActiveSheet.Name?
In a single-workbook scenario, they are identical. However, if multiple workbooks are open, ActiveSheet refers to the sheet active in the foreground Excel window, which may belong to a different workbook. ThisWorkbook.ActiveSheet always refers to the sheet active in the workbook where the code is stored, making it safer for reusable macros.
Can I use this to loop through all worksheets and check which one is active?
Yes. You can loop through ThisWorkbook.Worksheets and compare each sheet’s name to ActiveSheet.Name to find the active one, though it’s more efficient to use ActiveSheet directly unless you need to identify its index or position in the collection.
Is it possible to get the active worksheet name from another workbook?
Yes, but only if you activate that workbook first or reference it directly. For example: Workbooks("Budget.xlsx").ActiveSheet.Name will return the name of the active sheet in the Budget.xlsx workbook, provided it’s open.
Will this work in Excel for Mac or Excel Online?
The ActiveSheet.Name property works in Excel for Mac as part of VBA. However, Excel Online does not support VBA macros, so this method is not available there. For Excel Online, you would need to use Office Scripts (TypeScript-based) to achieve similar functionality.