To get a list of all sheet names in the current Excel workbook using VBA, you can use the ThisWorkbook.Sheets collection. This is useful for auditing, automation, or dynamic navigation within a workbook. You’ll need Excel with VBA enabled (not Excel Online) and basic familiarity with the VBA editor.
What You'll Need
- Microsoft Excel (desktop version: 2010 or later recommended)
- Excel file saved in a macro-enabled format (.xlsm or .xlsb) if you plan to save the macro
- Developer tab enabled in Excel (to access the VBA editor)
Step-by-Step
- Open the Excel workbook where you want to list the sheet names.
- Press Alt + F11 to open the Visual Basic for Applications (VBA) editor.
- In the VBA editor, go to Insert > Module to create a new standard module. A blank code window will appear on the right.
- Click anywhere inside the new module window, then type or paste the following code:
Sub ListSheetNames()
Dim ws As Worksheet
Dim outputRow As Long
Dim targetSheet As Worksheet
' Check if "SheetList" already exists; delete it to avoid duplicates
On Error Resume Next
Application.DisplayAlerts = False
Sheets("SheetList").Delete
Application.DisplayAlerts = True
On Error GoTo 0
' Create a new sheet named "SheetList" at the end of the workbook
Set targetSheet = ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
targetSheet.Name = "SheetList"
' Add header in cell A1
targetSheet.Range("A1").Value = "Sheet Name"
targetSheet.Range("A1").Font.Bold = True
' Start listing from row 2
outputRow = 2
' Loop through each worksheet in the current workbook
For Each ws In ThisWorkbook.Worksheets
targetSheet.Cells(outputRow, 1).Value = ws.Name
outputRow = outputRow + 1
Next ws
' Format column A to fit content
targetSheet.Columns("A").AutoFit
MsgBox "Sheet names listed in 'SheetList' sheet.", vbInformation
End Sub
- Press Ctrl + S to save the workbook. If prompted, choose Save as type: Excel Macro-Enabled Workbook (*.xlsm).
- Close the VBA editor by clicking the X or pressing Alt + Q.
- Ensure the Developer tab is visible in Excel (File > Options > Customize Ribbon > check Developer). If not, enable it.
- Go to the Developer tab on the Excel ribbon.
- Click Macros (in the Code group).
- In the Macro dialog box, select ListSheetNames from the list.
- Click Run.
- A new sheet named SheetList will appear at the end of your workbook, with a header row and all existing worksheet names listed in column A.
Common Mistakes
- Using ActiveWorkbook instead of ThisWorkbook: If other workbooks are open,
ActiveWorkbookmay reference the wrong file. Always useThisWorkbookto refer to the workbook containing the running macro. - Trying to rename a sheet to a reserved name: Names like "History" or "_xlnm#Consolidate_Area" are internal and cannot be assigned manually. Your code should avoid such names—or better, let Excel handle naming (e.g., use
Sheets.Addwithout setting a name if conflicts occur). - Forgetting to save as .xlsm: If saved as .xlsx, macros are stripped and the code won’t persist. Always verify the file type.
- Skipping error handling for duplicate sheet names: Attempting to add a sheet named "SheetList" when it already exists will throw an error. The provided code deletes it first using error handling.
- Overlooking hidden sheets: The
Worksheetscollection includes hidden, very hidden, and visible sheets. If you want only visible sheets, you must add a visibility check (e.g.,If ws.Visible = xlSheetVisible Then).
Tips
- To list sheet names in the current active cell range instead of a new sheet, replace the sheet-creation logic with a loop that writes to
ActiveCelland offsets downward. - For a quick debug view, use
Debug.Print ws.Nameinside the loop and press Ctrl + G in the VBA editor to open the Immediate Window. - Use
ThisWorkbook.Sheets.Countto get the total number of sheets (worksheets + chart sheets) before looping. - To include chart sheets, loop over
ThisWorkbook.Sheetsinstead ofWorksheets, but be aware chart sheets have different properties (e.g., no.Range). - Wrap the code in error handling (
On Error GoTo ErrorHandler) if distributing to users to prevent crashes and give helpful messages.
Troubleshooting
- Macro not running / "Macro not found": Ensure the workbook is saved as .xlsm. Check that the macro name in the list matches exactly (case-insensitive, but spelling must be identical). Try re-pasting the code and re-saving.
- Run-time error '9': Subscript out of range: Usually occurs when trying to delete the "SheetList" sheet if it doesn’t exist. The provided code handles this via
On Error Resume Next—if you removed that, restore it. - Sheet names appear blank or truncated: Verify the sheet names in the main workbook. Some names may contain leading/trailing spaces or non-printing characters. You can trim them with
Trim(ws.Name). - Macro security blocks execution: Go to File > Options > Trust Center > Trust Center Settings > Macro Settings. Select Enable all macros (temporarily, for testing) or Disable all macros with notification and click Enable Content when prompted upon opening the file.
- Excel crashes or becomes unresponsive: Press Ctrl + Break to stop the macro if stuck. Then check for infinite loops (unlikely here, but possible with nested loops) or memory issues (e.g., writing to millions of rows unnecessarily).
Frequently Asked Questions
Does this work in Excel Online or mobile?
No. VBA macros only run in the desktop version of Excel (Windows or macOS). Excel Online and mobile apps do not support VBA.Can I list sheet names without creating a new sheet?
Yes. Instead of adding "SheetList", you can write names to a specific range on an existing sheet. For example, replace the sheet-addition code withSheets("Summary").Range("A1").Resize(1, 1).Value = "Sheet Name" and loop into subsequent rows.
How do I list only visible sheets?
Modify the loop like this:For Each ws In ThisWorkbook.Worksheets
If ws.Visible = xlSheetVisible Then
targetSheet.Cells(outputRow, 1).Value = ws.Name
outputRow = outputRow + 1
End If
Next ws
Note: xlSheetVisible = -1. Avoid hardcoding numbers.
Why does the sheet name list include chart sheets?
The original code usesWorksheets, which excludes chart sheets. To include them, change ThisWorkbook.Worksheets to ThisWorkbook.Sheets and handle chart sheets separately if needed (e.g., TypeName(ws) to distinguish types).
Can I sort the sheet names alphabetically?
Yes. After listing, addtargetSheet.Sort.SortFields.Clear followed by sort setup code, or use Application.WorksheetFunction.Sort in newer Excel versions (365/2021) and write the sorted array to the sheet.
What if a sheet name has special characters (e.g., "/", "\", "[", "]", "*", "?", "")?
Excel itself prevents creating sheet names with those characters. If you see unexpected names, they may be very hidden or internal—usews.Visible checks to filter them out.