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.

Quick answer
- Save your workbook as .xlsm (macro-enabled).
- Press Alt+F11, choose Insert > Module and paste the code below (or File > Import File for the .txt file).
- Press Alt+F8, choose CreateTableOfContents and click Run.
- Click any sheet name on the new Contents sheet to jump to it. Run the macro again whenever the sheets change.
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.

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:
| Setting | Default | Meaning |
|---|---|---|
| TOC_NAME | "Contents" | Name of the sheet the macro creates or rebuilds |
| SHOW_HIDDEN | False | True also lists hidden sheets, marked (hidden) |

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.

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("#'Jan'!A1","Jan"). 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.

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.

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.

Variations
- Run it automatically: call CreateTableOfContents from Workbook_Open in ThisWorkbook.
- Add a back link: put =HYPERLINK("#'Contents'!A1","Back to contents") in a free cell on each sheet.
- Tidy the column widths on every sheet with autofit the used range with VBA.
- Made one sheet per region with the split data into multiple sheets macro? Run this macro afterwards to index them.
- Workbooks with one tab per task, like the month-end close checklist or the sales report dashboard, benefit most.
- Need one file instead of many? See combine multiple workbooks with VBA, or send the finished sheets with export a sheet to PDF with VBA.
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.