VBA macros · Sheets
VBA macro to list all sheet names in Excel
Write the name, position, type and visibility of every sheet in a workbook to a new sheet with a short VBA macro. Hidden sheets are included.
Sometimes you just need the sheet names as a list: to check a large workbook, to build a lookup, to plan a rename with VBA macro to rename sheets from a list in Excel or to paste the names into an email. This macro adds a new sheet at the front of the workbook and writes one row per sheet, including hidden and very hidden ones, so nothing slips past you.
Option Explicit
Sub ListAllSheetNames()
Dim wb As Workbook
Dim out As Worksheet
Dim n As Long, i As Long
Dim sh As Object
Dim state As String
Set wb = ActiveWorkbook
If wb Is Nothing Then Exit Sub
If wb.ProtectStructure Then
MsgBox "Unprotect the workbook structure first.", vbExclamation
Exit Sub
End If
n = wb.Sheets.Count ' count before adding the new sheet
Set out = wb.Worksheets.Add(Before:=wb.Sheets(1))
out.Range("A1:D1").Value = Array("No.", "Sheet name", "Type", "Visible")
out.Range("A1:D1").Font.Bold = True
out.Columns("B").NumberFormat = "@" ' keep names like 2026 as text
For i = 1 To n
Set sh = wb.Sheets(i + 1) ' +1 skips the new list sheet
Select Case sh.Visible
Case xlSheetVisible: state = "Visible"
Case xlSheetHidden: state = "Hidden"
Case xlSheetVeryHidden: state = "Very hidden"
End Select
out.Cells(i + 1, 1).Value = i
out.Cells(i + 1, 2).Value = sh.Name
out.Cells(i + 1, 3).Value = TypeName(sh)
out.Cells(i + 1, 4).Value = state
Next i
out.Columns("A:D").AutoFit
End SubHow to use it
- Open the workbook and press
Alt+F11(Mac:Opt+F11orFn+Opt+F11) to open the VBA editor. See The Developer tab and the VBA editor if you have never used it. - Choose Insert › Module. An empty code window opens on the right.
- Paste the whole code into that window. If the module already starts with
Option Explicit, do not paste that line twice. - Go back to Excel, press
Alt+F8(Mac:Opt+F8, or Tools › Macro › Macros), pickListAllSheetNamesand click Run. - Save the file as an Excel Macro-Enabled Workbook (.xlsm). A normal .xlsx file throws the code away when you save it.
To have this macro ready in every workbook, keep it in your Personal Macro Workbook (PERSONAL.XLSB). If you do not have one yet, record any tiny macro with Store macro in: Personal Macro Workbook (see The macro recorder: record, read and clean up code), then paste this code into a module under VBAProject (PERSONAL.XLSB). The macro works on whichever workbook is active when you run it.
How it works
The macro counts the sheets before it adds the output sheet, so the list does not include itself. The new sheet goes in front of all others, which means every original sheet moves one position to the right; that is why the loop reads wb.Sheets(i + 1) while writing the number i, which is the sheet's original position.
Writing all four headings at once with Range("A1:D1").Value = Array(...) is a handy shortcut: a one-row range accepts a one-dimensional array directly. Column B is formatted as text ("@") before the names are written, otherwise a sheet called 2026 or 001 would turn into a number and lose its leading zeros.
Select Case turns the numeric Visible value into words, and TypeName reports Worksheet or Chart. Both are covered in Making decisions: If, ElseIf and Select Case and Workbooks and worksheets in VBA.
Change it
If you only want the names in the column below the selected cell, without a new sheet, use this shorter version. It writes downwards from the active cell, so select an empty area first:
Sub ListSheetNamesHere()
Dim i As Long
For i = 1 To ActiveWorkbook.Sheets.Count
ActiveCell.Offset(i - 1, 0).Value = "'" & ActiveWorkbook.Sheets(i).Name
Next i
End SubThe apostrophe in front of each name forces text, the same job the "@" format does in the main macro. To list the sheets of another open workbook, replace ActiveWorkbook with Workbooks("Budget.xlsx"). To add clickable links as well, use VBA macro to create a table of contents with hyperlinks in Excel instead. In Microsoft 365 there is no worksheet function that lists sheet names, so a macro (or the old GET.WORKBOOK trick in a defined name) is the usual way.
What can go wrong
The list is a snapshot. If you add, rename or delete sheets afterwards, delete the list sheet and run the macro again. Each run adds a new sheet with a default name such as Sheet5; rename it if you want to keep it.
Related: VBA macro to create a table of contents with hyperlinks in Excel, VBA macro to rename sheets from a list in Excel.