Create Index Sheet

kevint504

New Member
Joined
Sep 7, 2004
Messages
25
I often find myself wanting to create an index sheet at the front of a workbook so I can notate what all the other sheets are for. I'd like to write a macro that will create the sheet and then list the names of all the other sheets, but I just don't know how. I've got as far as creating the index sheet and putting a title in cell A2:
Code:
Sheets.Add
    ActiveSheet.Name = "Index"
    Range("A2").Select
    ActiveCell.FormulaR1C1 = "Index"
So now I need some kind of For Next loop to list the titles of the other sheets.

A related (but perhaps not really excel) question - is there a way of creating an index sheet of all the workbooks in a particular folder? (Other than typing them by hand)
I'm running xl2000 and windows2000

many thanks for any help,

Kev
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Kev

For the first question.
Code:
Dim wsIndex As Worksheet
Dim ws As Worksheet
Dim I As Long

Set wsIndex = Worksheets.Add
wsIndex.Name = "Index"
wsIndex.Range("A2")="Index
I = 3
For Each ws In Worksheets
     If ws.Name<>wsIndex.Name Then
        wsIndex.Range("A"&I) = ws.Name
        I=I+1
     End If
Next ws

For the second I suggest you search the board for list files/workbooks.
 
Upvote 0
Sub GetSheets()
Dim q As Integer
Dim NumSheets As Integer

NumSheets = Sheets.Count
For q = 1 To NumSheets
Cells(q, 1) = Sheets(q).Name
Next q
End Sub

It overwrites column a, so you might want to run it on a blank sheet
 
Upvote 0
Check out Firefytr's code that create's a Table of Contents with hyperlinkls to each sheet:

<font face=Tahoma><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN>

<SPAN style="color:#00007F">Sub</SPAN> CreateTOC()
    <SPAN style="color:#007F00">'   Code by Zack Baresse</SPAN>
    <SPAN style="color:#00007F">If</SPAN> ActiveWorkbook <SPAN style="color:#00007F">Is</SPAN> <SPAN style="color:#00007F">Nothing</SPAN> <SPAN style="color:#00007F">Then</SPAN>
        MsgBox "You must have a workbook open first!", vbInformation, "No Open Book"
        <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
    
    <SPAN style="color:#00007F">With</SPAN> Application
        .ScreenUpdating = <SPAN style="color:#00007F">False</SPAN>
        .DisplayAlerts = <SPAN style="color:#00007F">False</SPAN>
    
        <SPAN style="color:#00007F">Dim</SPAN> ws <SPAN style="color:#00007F">As</SPAN> Worksheet, _
            ct <SPAN style="color:#00007F">As</SPAN> Chart, _
            shtName <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>, _
            nrow <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, _
            tmpCount <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, _
            i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, _
            numCharts <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>
        
        nrow = 3
        i = 1
        numCharts = ActiveWorkbook.Charts.Count
        
        <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> hasSheet
        Sheets("TOC").Activate
        <SPAN style="color:#00007F">If</SPAN> MsgBox("You already have a Table of Contents page.  Would you like to overwrite it?", _
        vbYesNo + vbQuestion, "Replace TOC page?") = vbYes <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> createNew
        <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN>

hasSheet:
    Sheets.Add Before:=Sheets(1)
    <SPAN style="color:#00007F">GoTo</SPAN> hasNew

createNew:
    Sheets("TOC").Delete
    <SPAN style="color:#00007F">GoTo</SPAN> hasSheet

hasNew:
    tmpCount = ActiveWorkbook.Charts.Count
    <SPAN style="color:#00007F">If</SPAN> tmpCount > 0 <SPAN style="color:#00007F">Then</SPAN> tmpCount = 1
        ActiveSheet.Name = "TOC"
        
        <SPAN style="color:#00007F">With</SPAN> Sheets("TOC")
            .Cells.Interior.Color<SPAN style="color:#00007F">In</SPAN>dex = 4
                <SPAN style="color:#00007F">With</SPAN> .Range("B2")
                    .Value = "Table of Contents"
                    .Font.Bold = <SPAN style="color:#00007F">True</SPAN>
                    .Font.Name = "Tahoma"
                    .Font.Size = "24"
                <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN>
        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN>
        
        <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> ws <SPAN style="color:#00007F">In</SPAN> ActiveWorkbook.Worksheets
            nrow = nrow + 1
            <SPAN style="color:#00007F">With</SPAN> ws
                shtName = ws.Name
                <SPAN style="color:#00007F">With</SPAN> Sheets("TOC")
                    .Range("B" & nrow).Value = nrow - 3
                    .Range("C" & nrow).Hyperlinks.Add _
                        Anchor:=Sheets("TOC").Range("C" & nrow), Address:="#'" & _
                        shtName & "'!A1", TextToDisplay:=shtName
                    .Range("C" & nrow).HorizontalAlignment = xlLeft
                <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN>
            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN>
        <SPAN style="color:#00007F">Next</SPAN> ws
        
        <SPAN style="color:#00007F">If</SPAN> numCharts <> 0 <SPAN style="color:#00007F">Then</SPAN>
            <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> ct In ActiveWorkbook.Charts
                nrow = nrow + 1
                shtName = ct.Name
                <SPAN style="color:#00007F">With</SPAN> Sheets("TOC")
                    .Range("B" & nrow).Value = nrow - 3
                    .Range("C" & nrow).Value = shtName
                    .Range("C" & nrow).HorizontalAlignment = xlLeft
                <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN>
            <SPAN style="color:#00007F">Next</SPAN> ct
        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
        
        <SPAN style="color:#00007F">With</SPAN> Sheets("TOC")
            <SPAN style="color:#00007F">With</SPAN> .Range("B2:G2")
                .MergeCells = <SPAN style="color:#00007F">True</SPAN>
                .HorizontalAlignment = xlLeft
            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN>
        
            <SPAN style="color:#00007F">With</SPAN> .Range("C:C")
                .EntireColumn.AutoFit
                .Activate
            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN>
            .Range("B4").Select
        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN>
    
        .DisplayAlerts = <SPAN style="color:#00007F">True</SPAN>
        .ScreenUpdating = <SPAN style="color:#00007F">True</SPAN>
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN>
    
    MsgBox "Done!" & vbNewLine & vbNewLine & "Please note: " & _
        "Charts are listed after regular " & vbCrLf & _
        "worksheets and will not have hyperlinks.", vbInformation, "Complete!"

<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
</FONT>

HTH,

Smitty
 
Upvote 0
Can I just say that that is EXCELLENT code.

I have added one teensy change to it under "hasnew:"

When constructing the TOC, my addition only looks for VISIBLE sheets:

Code:
        For Each ws In ActiveWorkbook.Worksheets
        
        If ws.Visible = xlSheetVisible Then
        
            nrow = nrow + 1
            With ws
                shtName = ws.Name
                With Sheets("TOC")
                    .Range("B" & nrow).Value = nrow - 3
                    .Range("C" & nrow).Hyperlinks.Add _
                        Anchor:=Sheets("TOC").Range("C" & nrow), Address:="#'" & _
                        shtName & "'!A1", TextToDisplay:=shtName
                    .Range("C" & nrow).HorizontalAlignment = xlLeft
                End With
            End With
            
        End If
        
        Next ws

My team now all love me for finding this... thanks pennysaver and (most of all) Firefytr!
 
Upvote 0

Forum statistics

Threads
1,214,980
Messages
6,122,563
Members
449,088
Latest member
Motoracer88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top