• Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Microsoft Excel Tips and Tricks - Computergaga

  • Home
  • Excel Online Courses
  • Excel Functions List
  • Free Downloads
  • Blog
  • Tutorials
  • About
Home / VBA / Automatically Create a Table of Contents in Excel

Automatically Create a Table of Contents in Excel

If a workbook contains many sheets you can create a table of contents to make navigating to the sheets easier. This is a fantastic idea when producing a final version of a report in Excel for a customer.

Excel does not yet contain a feature that produces a table of contents, but you can create a macro to get the job done.

This macro will create a new sheet at the start of the workbook named table of contents. If one already exists it will remove it. It will then list the names of all the sheets in the workbook and insert a hyperlink for each one.

Some of the techniques used within this macro include;

  • Using a counter loop on the sheets collection to loop through all the sheets of the workbook
  • Create and name a sheet during the macro
  • Insert hyperlinks for easier navigation of workbook content
  • Concatenation of strings for building the hyperlink address and working with a dynamic range
  • The ActiveCell object with Offset to move cells

Create a Table of Contents Macro Code

The VBA code is displayed below. Copy and paste it into the module of a workbook where you need a table of contents. This macro is designed to work in a file no matter how many sheets it contains, or what they are called.

Sub CreateTOC()
    Dim i As Byte
    Const SheetName = "Table of Contents"
    With Application
        .ScreenUpdating = False
        .DisplayAlerts = False
    End With
    If Sheets(1).Name = SheetName Then
        Sheets(SheetName).Delete
    End If
    Sheets.Add Before:=Sheets(1)
    Sheets(1).Name = SheetName
    Range("B2").Value = SheetName
    With Range("B2").Font
        .Name = "Calibri"
        .Size = 14
        .Underline = xlUnderlineStyleSingle
        .Bold = True
    End With
    Range("B4").Select
   'Loop through each sheet and create a table of contents using each sheet name
    For i = 2 To Sheets.Count
    ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:=Sheets(i).Name & "!A1", TextToDisplay:=i - 1 & ". " & Sheets(i).Name
    ActiveCell.Offset(2, 0).Select
    Next
    Range("B4:B" & ActiveCell.Row).Font.Underline = xlUnderlineStyleNone
    With Application
        .ScreenUpdating = True
        .DisplayAlerts = True
    End With
End Sub

Related Posts:

  • Advanced Excel Skills
    Advanced Excel Skills
  • Excel FILTER Function – The Best Function in Excel
    Excel FILTER Function – The Best Function in Excel
  • Create a Picture Lookup in Excel
    Create a Picture Lookup in Excel
  • Lookup Multiple Values in Excel
    Lookup Multiple Values in Excel

Reader Interactions

Comments

  1. mrsh says

    8 January 2014 at 10:52 am

    wonderfully helpful. kudos to you.

    Reply
    • Mrs. H. says

      18 July 2017 at 11:22 am

      This helped me for a long time. Suddenly the TOC began to justify CENTER instead of the desired LEFT justification.
      What went wrong and how to fix?
      Thanks.

      Reply
      • computergaga says

        20 July 2017 at 7:05 am

        I couldn’t say Mrs. H. Text by default in Excel is aligned to the left of a cell. Something must be positioning more centrally, but whether that be formatting, or hidden characters like spaces I couldn’t know.

        Reply
  2. Patrick PK says

    30 April 2014 at 4:08 pm

    Alan,
    Thank you, saved me a TON of work!!
    There is so much junk out there.
    Why doesn’t MS just give us the ability to add this as an add-in?
    Cheers!

    Reply
  3. ExcelRookie says

    2 June 2014 at 12:16 pm

    Hi Alan,
    The macro is working nicely in sheets where is no information. When I am trying to use macro in excel where is information in every sheets, your macro makes table of contents, but hyperlinks are not working. It says “Reference is no valid”. Could you advise? I am using excel 2010.

    Reply
    • computergaga_blog says

      2 June 2014 at 7:55 pm

      Its hard to say without seeing the file. Aslong as your workbook is structured as per the video and the code is the same (with changes where necessary). There should not be a problem.

      Reply
      • ExcelRookie says

        10 June 2014 at 6:50 am

        I think that the problem might be that my sheet names includes spaces, colors and some other special characters. Maybe I need to add some quotes, what do you think?

        Reply
        • ExcelRookie says

          10 June 2014 at 7:43 am

          Problem solved:
          ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:=””, SubAddress:=”‘” & Sheets(i).Name & “‘” & “!A1”, TextToDisplay:=i – 1 & “. ” & Sheets(i).Name
          ActiveCell.Offset(2, 0).Select

          Reply
          • computergaga_blog says

            11 June 2014 at 6:34 am

            Awesome work ExcelRookie

  4. Benjamin Haag says

    14 August 2014 at 3:40 pm

    This works beautifully, and saved me a considerable amount of time! Thank you!

    Reply
  5. CS says

    17 June 2015 at 10:32 am

    Hi
    this is brillant thnak you so much, has saved so much time and effort.
    one thing that does not work and that is if my tab has a space bar i get reference is not valid. i have tried the above link excelrookie posted but still cant get it to work.
    this is what i have? am i doing something wrong?
    ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:=””, SubAddress:=”‘” & Sheets(i).Name & “‘” & “!A1”, TextToDisplay:=i – 1 & “. ” & Sheets(i).Name
    ActiveCell.Offset(2, 0).Select
    thanks so much for you help
    CS

    Reply
    • computergaga says

      19 June 2015 at 6:48 am

      I’m not sure what you are doing wrong. I can only suggest it is the character you are using to enclose the sheet name in the Subaddress argument. It must be the single quote, or apostrophe.
      I tried it on mine, but instead of a direct copy and paste of your code I typed them in and it worked great.

      Reply
      • Terri Harford says

        4 July 2016 at 8:14 pm

        Hi I am also trying this script and also get the ‘reference is invalid’ which from reading above is to do with having spaces in my sheet names. You mention that it could be to do with the quotes – can you please identify which area you are referring to? and how I can rectify this?
        I ucopy and pasted this part of the code into the code from the top of this post: ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:=””, SubAddress:=”‘” & Sheets(i).Name & “‘” & “!A1”, TextToDisplay:=i – 1 & “. ” & Sheets(i).Name
        ActiveCell.Offset(2, 0).Select

        Reply
        • computergaga says

          12 July 2016 at 7:08 pm

          Hi Terri, sorry for the delay I have been away. Yes it does sound like the space issue. The statement should work and can be copied and pasted into the relevant section of your script.
          ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:=””, SubAddress:=”‘” & Sheets(i).Name & “‘!A1”, TextToDisplay:=i – 1 & “. ” & Sheets(i).Name
          ActiveCell.Offset(2, 0).Select

          Reply
  6. MelissaC says

    24 July 2015 at 1:50 pm

    This worked perfectly once I added the quotes that EXCEL rookie wrote. I had several sheets with spaces and dashes and needed the quotes. You might need to play with the spacing as I encountered a syntax error the first time I copied over the code from EXCEL rookie.

    Reply
  7. rachey says

    24 May 2016 at 4:53 pm

    mine does not like the Sub CreateTOC()
    I’m using 2013

    Reply
    • computergaga says

      25 May 2016 at 5:48 pm

      The version should not be an issue. I cannot understand why it would not like Sub CreateTOC(). What error does it show?

      Reply
  8. Rasmus Andreassen says

    9 June 2016 at 9:54 am

    Hi
    I was hoping that your macro would help me out aswel.
    But I also get the “reference is not valid”. Im on excel 2010.
    I have tried altering the code a bit without any luck(I have no experience with macro) It seems to make the TOC without any issues, but when I click the hyperlinks, only the 2nd and 3rd works, the rest comes with a warning.
    I have no idea what to do.

    Reply
    • computergaga says

      11 June 2016 at 3:36 pm

      Hi Rasmus,
      Hard to say without seeing what you have. The code provided should work fine, I would check it through.
      Kind regards
      Alan

      Reply
  9. Mrs. H. says

    13 June 2016 at 7:04 am

    This worked very well but now i get a MSVB error Run-time error ‘1004’: cannot rename a sheet to the same name as another sheet, a referenced object library or a workbook referenced by Visual Basic.
    Greek to me. can you please help?

    Reply
    • computergaga says

      13 June 2016 at 7:15 am

      Do you already have a Table of Contents sheet? Trying creating a sheet with different name. There is a name conflict somewhere.

      Reply
  10. Patty says

    24 June 2016 at 5:35 pm

    My links are not working. It says references are not working.
    However, If i add a new tab and run the macros again it works for the new sheet but not the old tabs.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Popular Posts

  • Excel Fixtures and League Table Generator
  • Excel IF Function Contains Text – A Partial Match in a Cell
  • 5 Reasons Why your Excel Formula is Not Calculating
  • Excel Formula to Display the Sheet Name in a Cell
  • 4 Alternatives to Nested IF Formulas
  • How to Create a Reverse FIND Formula
  • Check if a Value is an Even Number
  • How to Hyperlink to a Hidden Worksheet in Excel
  • IF Function in Power Query Including Nested IFS
  • Conditional Formatting Multiple Columns – 3 Examples

Recent Posts

  • SEQUENCE Function in Excel
  • Sort by Drop Down List in Excel
  • Advanced SUM Function Examples – The Power of SUM
  • Display Negative Time in Excel
  • Lookup Multiple Values in Excel
Advanced Excel Success book

Footer

Contact

[email protected]

Useful Links

  • Home
  • Excel Online Courses
  • Tutorials
  • Sitemap
  • Privacy Policy
  • Jobs by Jooble

Course Topics

  • Charts
  • Courses
  • Dashboards
  • Downloads
  • Excel Tips
  • Formulas
  • Mail Merge
  • Office
  • PivotTables
  • Power BI
  • Power Pivot
  • Power Query
  • VBA

Join Our Mailing List

© Copyright 2020 Computergaga · All Rights Reserved ·