- Community Pick
- Experts Exchange Approved
Introduction
This Article provides VBA code that will create a Table of Contents (TOC) summary sheet for all Sheets in the Active Workbook.
A TOC sheet provides a useful index for larger files, especially when they are shared between many users. I suggest that every Workbook should be saved with such a TOC active, and with the active cell set to A1 on all Worksheets — good practice to help avoid potential confusion and errors. It is highly recommended that having run this code that users (manually) add further descriptive text, colour coding, model maps etc to the TOC to designate sheet functionality (assumption sheets, input sheets, scenarios, outputs etc).
While most readers will look at this Article in order to use this code, coders may find the programmatic insertion of a Sheet event useful for other tasks.
TOC Outputs
The code produces a brief summary of the Workbook Name, the time the TOC was created, and the total number of Sheets in the TOC report.
Each individual sheet name is listed below the summary, these names act as a live index to the Sheets in the Workbook.
Two methods are used to create the links to the Active Workbook Sheets:
1) Simple hyperlinks are created for standard Worksheets.
2) Less commonly used Chart Sheets — and even rarer Dialog Sheets — cannot be hyperlinked. If this code detects a non-Worksheet type, a Sheet BeforeDoubleClick event is programmatically added to the TOC sheet so that these Sheets can still be referenced via a short cut.
Note that (2) requires that macros are enabled for this approach to work.
Understanding the code
There are 6 major portions in the sample code:
- Setting up the Excel environment by disabling screen updating, macros and alert messages
- The code tests whether there is an existing TOC sheet by looking for a "marker" range name, Toc_Index. If an existing summary sheet is detected the user is prompted to continue (with Yes & No buttons), pressing "Yes" will delete the current TOC. If the code has not exited then a new sheet is added to the front of the Active Workbook, it is renamed "TOC_Index" and the "marker" range name is added to cell A1 of this worksheet
- Each Sheet (other than the new TOC sheet) is tested for its Sheet type, standard worksheets are linked to the TOC sheet via hyperlinks and any other Sheet types are returned to the TOC sheet as values only. A boolean variable bNonWkSht, is used to capture the presence of any sheets that are not Worksheets
- Basic code sets up the TOC titles and TOC formatting
- If bNonWkSht is True then the Sheet Selection event is programmatically added to the TOC sheet along with a warning that macros must be enabled to provide automatic selection of Chart or Dialog Sheet types
- The Excel environment is restored by re-enabling screen updating, macros and alert messages
Using the code
1. Copy the code at the bottom of this Article
2. Open any workbook.
3. Press Alt + F11 to open the Visual Basic Editor (VBE).
4. From the Menu, choose Insert-Module.
5. Paste the code into the right-hand code window.
6. Close the VBE, save the file if desired.
In xl2003 go to Tools-Macro-Macros and double-click CreateTOC
In xl2007 click the Macros button in the Code group of the Developer tab, then click CreateTOC in the list box.
Please note that this code should be run from a regular VBA Code Module.
=-=-=-=-=-=-=-=-=-=-=-=-=-
If you liked this article and want to see more from this author, please click here.
And If you found the article helpful please click on the "Yes" button after the question below.
Regards
Dave
=-=-=-=-=-=-=-=-=-=-=-=-=-
by: bvanscoy678 on 2012-05-12 at 08:42:04ID: 53447
Thank you for the article! I have several charts that are highlighted in yellow in my TOC. I understand that charts can't be hyper linked and your comments suggest creating a short cut. I am not certain how to do this. I have enabled the macros.
Thank you,
Brent