Auto-generate a Table of Contents in Excel

Written by Shanx January 14th, 2004

Auto-generate a Table of Contents in Excel

Continue reading →
Close

Here’s a cool, handy macro to auto-generate a Table of Contents for any Excel file.

Here’s a cool, handy macro that will auto-generate a Table of Contents for any Excel file.

Steps remains the same as in any macro (Alt F11 to start VBA, Insert module, paste the code, save, File -> Return to Excel, then Alt F8, and Run). That’s a mouthful, but you know what to do.

Here’s the code:

Sub GenerateTableOfContents()
' Does a TOC already exist?
' If Err system variable is > 0, it doesn't
Dim wSheet As Worksheet
On Error Resume Next
Set wSheet = Worksheets("Table of Contents")
If Not Err = 0 Then

' The Table of contents doesn't exist. Add it
Set wSheet = Worksheets.Add(Before:=Worksheets(1))
wSheet.Name = "TOC"
End If
On Error GoTo 0

' Set up the table of contents page
wSheet.[A2] = "Table of Contents"

With wSheet.[A6]
.CurrentRegion.Clear
.Value = "Subject"
End With

wSheet.[B6] = "Page(s)"
wSheet.Range("A1:B1").ColumnWidth = Array(36, 12)
TableRow = 7
PageCount = 0
Worksheets.Select
displayMessage = "We'll do a Print Preview for some calculations."
displayMessage = displayMessage & "Please 'Close' the window when it appears."
MsgBox displayMessage
ActiveWindow.SelectedSheets.PrintPreview

' Now loop thru sheets, collecting TOC info
For Each S In Worksheets
S.Select
ThisName = S.Name
HPages = S.HPageBreaks.Count + 1
VPages = S.VPageBreaks.Count + 1
ThisPages = HPages * VPages

' Enter info about this sheet on TOC
wSheet.Cells(TableRow, 1).Value = ThisName
wSheet.Cells(TableRow, 2).NumberFormat = "@"
If ThisPages = 1 Then
  wSheet.Cells(TableRow, 2).Value =
  PageCount + 1 & " "
Else
  wSheet.Cells(TableRow, 2).Value =
  PageCount + 1 & " - " & PageCount + ThisPages
End If
PageCount = PageCount + ThisPages
TableRow = TableRow + 1
Next S
End Sub

That’s all there is to it! Note that VBA does not allow putting the lines after an equal to sign (” = “) on a separate line, although the above code has them so (only to save formatting.)

Feel free to leave a note if you don’t understand some bit of the code and I’ll try to explain, though it’s quite self-explanatory. The calculation of the number of pages is done through the number of page breaks inside the Print Preview.

Have fun!

8 Comments

  1. Emmanuel says:

    This is really really useful, thanks!

  2. toto says:

    IN VBA code you can continue on more than one lines with the underbar chatacter.

    _

    E.g.,

    displayMessage = _
    “We’ll do a Print Preview for some _
    calculations.” _
    displayMessage = displayMessage & _
    “Please ‘Close’ the window when it _
    appears.”

    This should work. Just like in Visual Basic.

  3. A nondescript Googler says:

    I found useful code usage here – that I couldn’t even find on Usenet. Nice work.

  4. John says:

    Definately saved me a few hours so thanks a lot

  5. mark says:

    I am getting a compile error on the wsheet function. i am using excel 2003 fully service packed. what am i doing wrong?

    If ThisPages = 1 Then
    wSheet.Cells(TableRow, 2).Value = PageCount + 1&” ”
    Else
    wSheet.Cells(TableRow, 2).Value = PageCount + 1&” – “&PageCount + ThisPages

  6. Rachelle says:

    This is simply wonderful!!!

  7. Launcher says:

    I am running Excel 2000 with Visual Basic 6.0

    When I run this code I get the following error:
    Compile Error:
    Syntax Error

    Plus, the following line is highlighted:
    ’ Does a TOC already exist?

    Also, there are a number of lines that are displayed in red text.

    Can someone please help…I know nothing about codes, Visual Basic nor macros

  8. Jason says:

    The program works. Thank you. I, however, constantly get an additional sheet added prior to the TOC page (e.g. Sheet 1) and it is listed on the TOC as page 1. Is there a way to alter the code to eliminate this from happening?

Leave a Reply

Miscellaneous

I use the Nokia e61i as my mobile. Instead of my telco’s data plan (which offers me a meagre 1GB per month) I simply prefer to use my home wireless [...]

Continue reading →

View all

Web Tools

If you use Firefox (and if not, what are you waiting for?) you are familiar with useful extensions such as Video Downloader, which allow you to save local copies of [...]

Continue reading →

View all

Databases

This regexp worked for me. SELECT * FROM table WHERE NOT column ~ ( ‘^(‘|| $$[\09\0A\0D\x20-\x7E]|$$|| — ASCII $$[\xC2-\xDF][\x80-\xBF]|$$|| — non-overlong 2-byte $$\xE0[\xA0-\xBF][\x80-\xBF]|$$|| — excluding overlongs $$[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}|$$|| — straight 3-byte [...]

Continue reading →

View all

Windows

So you’ve been visited by the much dreaded CRC — Cyclical Redundancy Check error, most likely encountered while copying files between hard disks. On Mac OSX, this will usually appear [...]

Continue reading →

View all

Mac OSX

A simple app ought to do it. Download iRinger. It’s a Windows app. If you’re on Mac, you’ll want to use it within a virtual machine, like Parallels or VMWare [...]

Continue reading →

View all

System Maintenance

I use the Nokia e61i as my mobile. Instead of my telco’s data plan (which offers me a meagre 1GB per month) I simply prefer to use my home wireless [...]

Continue reading →

View all

Wordpress

Among many new exciting features, WordPress 2.6 released the ability to store each and every revision of your posts, like an elaborate update history. Now this can be a pretty [...]

Continue reading →

View all

Audio/Video

Panic, the makers of some fantastic software such as Transmit or Panic, also have the most light-weight audio converter for the Mac OSX platform. It’s called Audion: get it here. [...]

Continue reading →

View all

iPhone

A simple app ought to do it. Download iRinger. It’s a Windows app. If you’re on Mac, you’ll want to use it within a virtual machine, like Parallels or VMWare [...]

Continue reading →

View all