Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

CURRENTLY:

I got this macro online that adds a Table of Contents worksheet to easily navigate to any tab.

My workbook:

Macro create Contents tab with table of contents:

THE IDEA:
a. Improve the table of contents so it will have sections, and also will display text notes
together with the worksheet name. Notes does not need to be clickable (link)
b. Freze the Contents worksheet position, even if I have several worksheets and scroll to
to the sides, the Table of Contents worksheet will be always visible.

I want to have some like this:

Sections created based on


each tab content

Open
Project delayed due to budget constraints

Closed Won
Will call next week to sign agreement

Notes will be here also from


each tab content

Closed Lost
Lost to Xyz company, on bid war

Content of a worksheet:

Notes here also will be


displayed in the table of
contents.

I want a drop-down here to


choose between Open,
Closed Won and Closed Lost.
Based on that this worksheet
will be displayed in the table
of content.

This tab should be fixed. If I scroll to the side to see other tabs, it will aways be visible

Current Macro:

Sub TableOfContents_Create()

Dim sht As Worksheet


Dim Content_sht As Worksheet
Dim myArray As Variant
Dim x As Long, y As Long, z As Long
Dim shtName1 As String, shtName2 As String
Dim ContentName As String
Dim shtCount As Long
Dim ColumnCount As Variant

'Inputs
ContentName = "Contents"

'Optimize Code
Application.DisplayAlerts = False
Application.ScreenUpdating = False

'Delete Contents Sheet if it already exists


On Error Resume Next
Worksheets("Contents").Activate
On Error GoTo 0

If ActiveSheet.Name = ContentName Then


myAnswer = MsgBox("A worksheet named [" & ContentName & _
"] has already been created, would you like to replace it?", vbYesNo)

'Did user select No or Cancel?


If myAnswer <> vbYes Then GoTo ExitSub

'Delete old Contents Tab


Worksheets(ContentName).Delete
End If

'Count how many Visible sheets there are


For Each sht In ActiveWorkbook.Worksheets
If sht.Visible = True Then shtCount = shtCount + 1
Next sht

'Ask how many columns to have


ColumnCount = Application.InputBox("You have " & shtCount & _
" visible worksheets." & vbNewLine & "How many columns " & _
"would you like to have in your Contents tab?", Type:=2)

'Check if user cancelled


If TypeName(ColumnCount) = "Boolean" Or ColumnCount < 0 Then GoTo ExitSub

'Create New Contents Sheet


Worksheets.Add Before:=Worksheets(1)

'Set variable to Contents Sheet and Rename


Set Content_sht = ActiveSheet
Content_sht.Name = ContentName

'Create Array list with sheet names (excluding Contents)


ReDim myArray(1 To shtCount)

For Each sht In ActiveWorkbook.Worksheets


If sht.Name <> ContentName And sht.Visible = True Then
myArray(x + 1) = sht.Name

x=x+1
End If
Next sht

'Alphabetize Sheet Names in Array List


For x = LBound(myArray) To UBound(myArray)
For y = x To UBound(myArray)
If UCase(myArray(y)) < UCase(myArray(x)) Then
shtName1 = myArray(x)
shtName2 = myArray(y)
myArray(x) = shtName2
myArray(y) = shtName1
End If
Next y
Next x

'Create Table of Contents


x=1

For y = 1 To ColumnCount
For z = 1 To WorksheetFunction.RoundUp(shtCount / ColumnCount, 0)
If x <= UBound(myArray) Then
Set sht = Worksheets(myArray(x))
sht.Activate
With Content_sht
.Hyperlinks.Add .Cells(z + 2, 2 * y), "", _
SubAddress:="'" & sht.Name & "'!A1", _
TextToDisplay:=sht.Name
End With
x=x+1
End If

Next z
Next y

'Select Content Sheet and clean up a little bit


Content_sht.Activate
Content_sht.UsedRange.EntireColumn.AutoFit
ActiveWindow.DisplayGridlines = False

'Format Contents Sheet Title


With Content_sht.Range("B1")
.Value = "ndice"
.Font.Bold = True
.Font.Size = 18
End With

ExitSub:
'Optimize Code
Application.DisplayAlerts = True
Application.ScreenUpdating = True

End Sub

You might also like