EXCEL AUTOMATION & VBA

Create a Table of Contents in Excel with VBA (Clickable Sheet Links)

A workbook with 20 tabs is hard to move around in: the tab you need is always off screen. A Contents sheet at the front with a link to every sheet fixes that. This free macro builds it in one click, takes each sheet’s title from cell A1, skips hidden sheets and rebuilds the list in place when you add, rename or reorder sheets.

Excel Contents sheet made with a VBA macro, with a HYPERLINK formula for each sheet name and 7 sheets listed.
One link per sheet, built by a macro.

Quick answer

  1. Save your workbook as .xlsm (macro-enabled).
  2. Press Alt+F11, choose Insert > Module and paste the code below (or File > Import File for the .txt file).
  3. Press Alt+F8, choose CreateTableOfContents and click Run.
  4. Click any sheet name on the new Contents sheet to jump to it. Run the macro again whenever the sheets change.
Free download: Get the sample workbook (.xlsx) and the macro (create-table-of-contents.txt). The sample has 8 sheets, one of them hidden and one with an apostrophe in its name, so you can see how the macro handles both. Works in Excel 2010 and later for Windows and Mac. VBA does not run in Excel for the web.
Before you run it: Save a copy first; macros cannot be undone with Ctrl+Z. If your workbook already has a sheet called Contents, the macro clears and reuses it. Change TOC_NAME if that sheet holds something else.

Step 1: Give each sheet a title in A1

The macro lists every visible worksheet in tab order. For the Title column it reads cell A1 of each sheet, so a short description there (January sales by region, Notes for the owner) makes the Contents sheet much more useful than tab names alone. Sheets with an empty A1 are still listed, just without a title. The sample has seven visible sheets and a hidden Lookup sheet.

Excel workbook with sheet tabs Summary, Jan, Feb, Mar, Q1 Chart data, Owner's notes and How It Works, with the sheet title in cell A1.
Seven visible sheets, titles in A1.

Step 2: Paste the macro

Press Alt+F11 to open the VBA editor, select your workbook in the Project pane, choose Insert > Module and paste the full code:

Option Explicit

' OneXcel Studio | Table of contents with a hyperlink to every sheet
' Creates (or rebuilds) a "Contents" sheet at the front of the workbook.

Private Const TOC_NAME As String = "Contents"   ' name of the contents sheet
Private Const SHOW_HIDDEN As Boolean = False     ' True = list hidden sheets too

Public Sub CreateTableOfContents()
    Dim wb As Workbook, toc As Worksheet, ws As Worksheet
    Dim r As Long, n As Long, target As String, lastRow As Long

    Set wb = ThisWorkbook
    Application.ScreenUpdating = False

    ' 1. Reuse the Contents sheet if it exists, otherwise add it at the front
    On Error Resume Next
    Set toc = wb.Worksheets(TOC_NAME)
    On Error GoTo 0
    If toc Is Nothing Then
        Set toc = wb.Worksheets.Add(Before:=wb.Worksheets(1))
        toc.Name = TOC_NAME
    Else
        toc.Move Before:=wb.Worksheets(1)
        toc.Cells.Clear
    End If

    toc.Range("A1").Value = "Contents"
    toc.Range("A1").Font.Size = 16
    toc.Range("A1").Font.Bold = True
    toc.Range("A3:D3").Value = Array("No.", "Sheet", "Title (cell A1)", "Rows used")
    toc.Range("A3:D3").Font.Bold = True
    r = 3

    ' 2. One row per sheet, with a HYPERLINK formula to cell A1 of that sheet
    For Each ws In wb.Worksheets
        If ws.Name <> TOC_NAME Then
            If ws.Visible = xlSheetVisible Or SHOW_HIDDEN Then
                r = r + 1: n = n + 1
                toc.Cells(r, 1).Value = n
                ' quote the name so spaces and apostrophes work: #'Owner''s notes'!A1
                target = "#'" & Replace(ws.Name, "'", "''") & "'!A1"
                toc.Cells(r, 2).Formula = "=HYPERLINK(""" & target & """,""" & _
                    Replace(ws.Name, """", """""") & """)"
                toc.Cells(r, 3).Value = CStr(ws.Range("A1").Value)
                lastRow = ws.UsedRange.Row + ws.UsedRange.Rows.Count - 1
                If Application.WorksheetFunction.CountA(ws.Cells) = 0 Then lastRow = 0
                toc.Cells(r, 4).Value = lastRow
                If ws.Visible <> xlSheetVisible Then _
                    toc.Cells(r, 3).Value = toc.Cells(r, 3).Value & " (hidden)"
            End If
        End If
    Next ws

    ' 3. Tidy up
    toc.Cells(r + 2, 1).Value = n & " sheets listed. Run the macro again after adding or renaming sheets."
    toc.Cells(r + 2, 1).Font.Italic = True
    toc.Columns("A:D").AutoFit
    toc.Activate
    toc.Range("B4").Select
    Application.ScreenUpdating = True
End Sub

There are only two settings:

SettingDefaultMeaning
TOC_NAME"Contents"Name of the sheet the macro creates or rebuilds
SHOW_HIDDENFalseTrue also lists hidden sheets, marked (hidden)
VBA editor showing the two settings at the top of the module: TOC_NAME set to Contents and SHOW_HIDDEN set to False.
Two settings at the top.

Step 3: Why a re-run never duplicates the sheet

    ' 1. Reuse the Contents sheet if it exists, otherwise add it at the front
    On Error Resume Next
    Set toc = wb.Worksheets(TOC_NAME)
    On Error GoTo 0
    If toc Is Nothing Then
        Set toc = wb.Worksheets.Add(Before:=wb.Worksheets(1))
        toc.Name = TOC_NAME
    Else
        toc.Move Before:=wb.Worksheets(1)
        toc.Cells.Clear
    End If

The macro first tries to get a sheet called Contents. On Error Resume Next stops the error when it does not exist, and toc stays Nothing, so a new sheet is added before the first sheet. If Contents already exists, it is moved back to the front and cleared. That is what makes the macro safe to run as often as you like.

VBA code that reuses the Contents sheet if it exists or adds it at the front, then writes the header row No., Sheet, Title and Rows used.
Reuse or add, then write the headers.

Step 4: How the links are built

    ' 2. One row per sheet, with a HYPERLINK formula to cell A1 of that sheet
    For Each ws In wb.Worksheets
        If ws.Name <> TOC_NAME Then
            If ws.Visible = xlSheetVisible Or SHOW_HIDDEN Then
                r = r + 1: n = n + 1
                toc.Cells(r, 1).Value = n
                ' quote the name so spaces and apostrophes work: #'Owner''s notes'!A1
                target = "#'" & Replace(ws.Name, "'", "''") & "'!A1"
                toc.Cells(r, 2).Formula = "=HYPERLINK(""" & target & """,""" & _
                    Replace(ws.Name, """", """""") & """)"
                toc.Cells(r, 3).Value = CStr(ws.Range("A1").Value)
                lastRow = ws.UsedRange.Row + ws.UsedRange.Rows.Count - 1
                If Application.WorksheetFunction.CountA(ws.Cells) = 0 Then lastRow = 0
                toc.Cells(r, 4).Value = lastRow
                If ws.Visible <> xlSheetVisible Then _
                    toc.Cells(r, 3).Value = toc.Cells(r, 3).Value & " (hidden)"
            End If
        End If
    Next ws

For each sheet the macro writes a HYPERLINK formula such as =HYPERLINK(&quot;#&#39;Jan&#39;!A1&quot;,&quot;Jan&quot;). The # tells Excel the link points inside this workbook. The sheet name is wrapped in single quotes, and any apostrophe in the name is doubled, so names like Q1 Chart data and Owner's notes work. Because the link is a formula, it keeps working if you rename the file or save it in another folder. The Rows used column shows the last used row of each sheet, a quick way to spot a sheet that has grown unexpectedly.

VBA code that loops through the worksheets, skips hidden ones and writes a HYPERLINK formula with the sheet name in single quotes.
Quoted sheet names, one formula per row.

Step 5: Run it and click a link

Press Alt+F8, choose CreateTableOfContents and click Run. The sample gets a Contents sheet with 7 rows: Summary (14 rows used), Jan (22), Feb (20), Mar (24), Q1 Chart data (13), Owner's notes (9) and How It Works (12). Lookup is hidden, so it is skipped. Click any name in column B to jump to cell A1 of that sheet. To get back, add a link to the Contents sheet on each sheet or use the sheet list described in find any sheet fast in Excel.

Contents sheet created by the macro with 7 clickable sheet names, the title from cell A1 of each sheet and the last used row.
7 sheets, each one click away.

Step 6: Add a sheet and run it again

The list does not update by itself: run the macro after adding, renaming, hiding or moving sheets. The old list is cleared and rebuilt in place, in the new tab order.

Contents sheet after adding an Apr sheet and running the macro again, still one Contents sheet, now listing 8 sheets with Apr in row 8.
Still one Contents sheet, now 8 rows.
Try it: Run the macro, add a sheet called Apr after Mar (with April sales by region in A1) and run it again. There is still only one Contents sheet, Apr appears as number 5 and the note at the bottom says 8 sheets listed. Set SHOW_HIDDEN = True and run it on the sample: Lookup is added as Region and rep lookup table (hidden) and the count is 8 again.
Hidden sheets: A link to a hidden sheet does nothing when you click it, because Excel cannot select a hidden sheet. SHOW_HIDDEN = True is useful for an inventory of the workbook; unhide the sheet (right-click a tab, Unhide) to use the link.

Variations

Troubleshooting

  • Nothing happens: macros are disabled. Save as .xlsm and enable content, or check File > Options > Trust Center.
  • Reference isn't valid when clicking a link: the sheet was renamed or deleted after the last run. Run the macro again.
  • Run-time error 1004 when the sheet is added: the workbook structure is protected (Review > Protect Workbook). Unprotect it first.
  • Chart sheets are missing: the macro loops through Worksheets, so separate chart sheets are not listed. Charts placed on a worksheet are fine.
  • Rows used looks too high: a cell far down once had formatting. Clear the empty rows below the data and save.

Frequently asked questions

How do I create a table of contents in Excel without VBA?

Type the sheet names and use Insert > Link > Place in This Document for each one, or HYPERLINK formulas. It works, but you have to update it by hand every time a sheet is added or renamed.

Does the macro change my other sheets?

No. It only reads cell A1 and the used range of each sheet. It writes only to the Contents sheet.

Can I keep the macro in my Personal Macro Workbook?

Yes, but change ThisWorkbook to ActiveWorkbook so it builds the list in the workbook you are looking at, not in PERSONAL.XLSB.

Will the links work after I save the file as .xlsx?

Yes. The links are ordinary HYPERLINK formulas. Only the macro is removed when you save as .xlsx.

Does it work on a Mac?

Yes, in Excel for Mac 2016 and later. It does not run in Excel for the web, which does not support VBA.

Final checklist

  • File saved as .xlsm, with a backup copy.
  • Each sheet has a short title in A1.
  • Macro run with Alt+F8.
  • Contents sheet count matches the visible sheets.
  • Macro run again after adding or renaming sheets.

Automate the repetitive parts

Find more ready-to-use Excel templates, learn the formulas in the OneXcel Tutorial Hub, or turn your sheets into a report with the OneXcel Dashboard Builder.