Option Explicit
Sub create_TOC()
'Erik Van Geit
'060906
Dim i As Integer
Dim msg As String
Dim fc_order As Range
Dim fc_alphabet As Range
Dim sht As Object
'**** EDIT the following lines ****
Const TOC = "Table of Contents"
Const TocShort = "TOC"
Const CellLink = "A1"
'**** END EDIT ****
On Error Resume Next
msg = Sheets(TOC).Name
If Err Then
Err.Clear
msg = "A new sheet will be added :""" & TOC & """, with hyperlinks to all sheets in this workbook."
Else
Worksheets(TOC).Activate
msg = Chr(10) & Chr(10) & "Your sheet " & Chr(10) & TOC & Chr(10) & _
"(now displayed) will be updated."
End If
On Error GoTo 0
If MsgBox(msg & Chr(10) & "Do you want to continue ?", 36, TOC) = vbNo Then Exit Sub
Application.ScreenUpdating = False
Application.DisplayAlerts = False
On Error Resume Next
Worksheets(TOC).Delete
On Error GoTo 0
Worksheets.Add before:=Sheets(1)
With Sheets(1)
.Name = TOC
.Cells.Interior.ColorIndex = 15
ActiveWindow.DisplayHeadings = False
With .Cells(2, 6)
.Value = UCase(TOC)
.Font.Size = 18
.HorizontalAlignment = xlCenter
End With
Set fc_order = .Cells(3, 4)
Set fc_alphabet = .Cells(3, 8)
fc_order = "order of appearance"
For i = 2 To ActiveWorkbook.Worksheets.Count
.Hyperlinks.Add Anchor:=Cells(i + 2, 4), Address:="", _
SubAddress:=Worksheets(i).Name & "!A1", TextToDisplay:=Worksheets(i).Name
Next i
fc_alphabet = "alphabetically"
.Range(fc_order.Offset(1, 0), fc_order.End(xlDown)).Copy fc_alphabet.Offset(1, 0)
.Range(fc_alphabet.Offset(1, 0), fc_alphabet.End(xlDown)).Sort Key1:=fc_alphabet.Offset(1, 0)
End With
msg = "Do you want a hyperlink to " & TOC & " on each sheet in cell " & CellLink & "?" & Chr(10) & _
"(if cell " & CellLink & " is empty)"
If MsgBox(msg, 36, "Hyperlink on each sheet") = vbYes Then
For Each sht In Worksheets
With sht
If .Range(CellLink) = "" And sht.Name <> TOC Then
.Unprotect
.Hyperlinks.Add Anchor:=.Range(CellLink), Address:="", _
SubAddress:="'" & TOC & "'!A1", TextToDisplay:=TocShort
.Protect
End If
End With
Next sht
End If
Sheets(TOC).Activate
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub