Sometimes workbooks can be very large and hard to navigate. Only so many tabs fit across the bottom of the screen, and it’s hard to know how long each worksheet is. Excel doesn’t have a built-in way to add a table of contents to a workbook; however, there is a way! In this post, I’ll show you how to add a new worksheet to the beginning of your workbook called “TOC” (table of contents). This example uses Excel 2010.
On the TOC sheet, Column A lists each sheet name and includes a hyperlink shortcut to the appropriate worksheet. Column B lists the worksheet number (which sheet it is in the workbook) and the number of pages contained on that worksheet (how many printed pages it would be). You have to use VBA (Visual Basic for Applications), but don’t be scared off by the fact that you need code—it’s actually pretty simple, and I’ll walk you through it step-by-step.
First, you have to add the code to your workbook, and to do that you need the Developer tab. If you don’t usually work with code in Excel, you probably don’t see the Developer tab in the Ribbon. To show the Developer tab:
- Click the File tab.
- Under Help, click Options.
- Click Customize Ribbon.
- Under Customize the Ribbon, select the Developer check box.
Now you can create a macro:
- On the Developer tab, in the Code group, click Visual Basic.
- In the Visual Basic Editor, on the Insert menu, click Module.
- In the code window of the module, type or copy the following macro code:
Dim wbBook As Workbook
Dim wsActive As Worksheet
Dim wsSheet As Worksheet
Dim lnRow As Long
Dim lnPages As Long
Dim lnCount As Long
Set wbBook = ActiveWorkbook
.DisplayAlerts = False
.ScreenUpdating = False
‘If the TOC sheet already exist delete it and add a new
On Error Resume Next
On Error GoTo 0
Set wsActive = wbBook.ActiveSheet
.Name = “TOC”
.Value = VBA.Array(“Table of Contents”, “Sheet # – # of Pages”)
.Font.Bold = True
lnRow = 2
lnCount = 1
‘Iterate through the worksheets in the workbook and create
‘sheetnames, add hyperlink and count & write the running number
‘of pages to be printed for each sheet on the TOC sheet.
For Each wsSheet In wbBook.Worksheets
If wsSheet.Name <> wsActive.Name Then
.Hyperlinks.Add .Cells(lnRow, 1), “”, _
SubAddress:=”‘” & wsSheet.Name & “‘!A1″, _
lnPages = wsSheet.PageSetup.Pages().Count
.Cells(lnRow, 2).Value = “‘” & lnCount & “-” & lnPages
lnRow = lnRow + 1
lnCount = lnCount + 1
.DisplayAlerts = True
.ScreenUpdating = True
Now, save your workbook with the macro code.
- In the Visual Basic Editor, on the File menu, click Close and Return to Microsoft Excel.
- On the File menu, click Save As, and then save the file as an Excel Macro-Enabled Workbook (.xlsm).
You are almost ready to run the code. You might need to change your macro security settings to enable macros. To set the security level temporarily to enable all macros, do the following:
- On the Developer tab, in the Code group, click Macro Security.
- Under Macro Settings, click Enable all macros (not recommended, potentially dangerous code can run), and then click OK.
Note To help prevent potentially dangerous code from running, we recommend that you return to any one of the settings that disable all macros after you finish working with macros.
And now you can run the code that creates the table of contents worksheet!
- On the Developer tab, in the Code group, click Macros.
- In the Macro window, select the Create_TOC macro, and click Run.