VBA macros · Sheets
VBA macro to create a table of contents with hyperlinks in Excel
Build a Contents sheet that lists every sheet with a clickable hyperlink. Run it again any time to refresh the list after adding or renaming sheets.
A workbook with thirty sheets is much easier to use when the first sheet is a clickable index. Building one by hand means inserting thirty hyperlinks and redoing them every time a sheet is renamed. The macro below creates (or refreshes) a sheet called Contents at the front of the workbook with one link per visible sheet.
Option Explicit
Sub CreateTableOfContents()
Const TOC_NAME As String = "Contents"
Dim wb As Workbook
Dim toc As Worksheet
Dim sh As Object
Dim r As Long
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
On Error GoTo Fail
Application.ScreenUpdating = False
' 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 Fail
If toc Is Nothing Then
Set toc = wb.Worksheets.Add(Before:=wb.Sheets(1))
toc.Name = TOC_NAME
Else
toc.Hyperlinks.Delete
toc.Cells.Clear
End If
With toc.Range("A1")
.Value = "Contents"
.Font.Bold = True
.Font.Size = 14
End With
r = 3
For Each sh In wb.Sheets
If sh.Name <> TOC_NAME And sh.Visible = xlSheetVisible Then
If TypeName(sh) = "Worksheet" Then
' Quotes around the name make spaces and symbols safe
toc.Hyperlinks.Add Anchor:=toc.Cells(r, 1), Address:="", _
SubAddress:="'" & Replace(sh.Name, "'", "''") & "'!A1", _
TextToDisplay:=sh.Name
Else
toc.Cells(r, 1).Value = sh.Name & " (chart sheet)"
End If
r = r + 1
End If
Next sh
toc.Columns(1).AutoFit
toc.Activate
Application.ScreenUpdating = True
Exit Sub
Fail:
Application.ScreenUpdating = True
MsgBox "Could not build the contents: " & Err.Description, vbExclamation
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), pickCreateTableOfContentsand 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 first tries to grab an existing sheet called Contents. On Error Resume Next lets that line fail quietly when the sheet does not exist, in which case toc stays Nothing and a new sheet is added in front of the first sheet. If the sheet does exist, its old links and contents are cleared so the list is rebuilt from scratch, which is what makes the macro safe to run again and again.
Each link is created with Hyperlinks.Add. For a link inside the workbook, Address is empty and SubAddress holds the target, written exactly like a cell reference in a formula: 'Sales 2026'!A1. The single quotes around the name are needed for names with spaces or dashes, and Replace(sh.Name, "'", "''") doubles any apostrophe inside a name such as Bob's sheet, which is how Excel escapes it.
Hyperlinks can only jump to cells, so chart sheets get a plain text entry instead, and hidden sheets are skipped because a link to a hidden sheet does nothing when clicked. TypeName(sh) returns "Worksheet" or "Chart" and makes that distinction easy.
Change it
A Back to contents link on every sheet makes navigation work in both directions. Add these lines inside the If TypeName(sh) = "Worksheet" branch; the link goes into a cell you choose, here Z1, so it does not overwrite data:
sh.Hyperlinks.Add Anchor:=sh.Range("Z1"), Address:="", _
SubAddress:="'" & TOC_NAME & "'!A1", _
TextToDisplay:="Back to contents"To show a value next to each link, for example the total in cell F20 of every sheet, add toc.Cells(r, 2).Formula = "='" & Replace(sh.Name, "'", "''") & "'!F20". Because it is a live formula, the number updates by itself. To include hidden sheets as plain text, change the visibility test into an Else branch that writes the name with (hidden) after it.
What can go wrong
TOC_NAME to a name that cannot clash with a sheet you use for something else.If you rename a sheet later, existing links do not follow the new name; they show Reference isn't valid when clicked. Run the macro again after renaming or adding sheets. A protected workbook structure blocks adding the sheet, which is why the macro checks it first.
Related: VBA macro to list all sheet names in Excel, VBA macro to sort sheets alphabetically in Excel, Workbooks and worksheets in VBA.