I am brand new to VBA Code. I have a workbook with many worksheets, and I want to add an index.
I tried advice from this website: Use index sheets to quickly navigate in an Excel workbook
"To create the index, follow these steps:
****I followed the instructions. When I reopened the workbook, the index did not appear. Nothing happened. I do not know what they mean by "Listing A". When I right-clicked on the sheet tab and selected "view code," a box automatically appeared where I could place code so that's where I put it. The box had a title of "workbookname.xlsm - Sheet268 (Code)". There are 2 dropdown menus at the top-- one says "worksheet," the other says "activate." Also, I am not sure if the suggested code was like a template and you are supposed to add something to it? I copied the code "as is."***
The suggested code is:
I tried advice from this website: Use index sheets to quickly navigate in an Excel workbook
"To create the index, follow these steps:
- Insert a new worksheet at the beginning of your workbook and rename it Index.
- Right-click on the sheet tab and select View Code.
- Enter the following code in Listing A.
- Press [Alt][Q] and save the workbook.
****I followed the instructions. When I reopened the workbook, the index did not appear. Nothing happened. I do not know what they mean by "Listing A". When I right-clicked on the sheet tab and selected "view code," a box automatically appeared where I could place code so that's where I put it. The box had a title of "workbookname.xlsm - Sheet268 (Code)". There are 2 dropdown menus at the top-- one says "worksheet," the other says "activate." Also, I am not sure if the suggested code was like a template and you are supposed to add something to it? I copied the code "as is."***
The suggested code is:
VBA Code:
Private Sub Worksheet_Activate()
Dim wSheet As Worksheet
Dim M As Long
M = 1
With Me
.Columns(1).ClearContents
.Cells(1, 1) = "INDEX"
.Cells(1, 1).Name = "Index"
End With
For Each wSheet In Worksheets
If wSheet.Name <> Me.Name Then
M = M + 1
With wSheet
.Range("H1").Name = "Start" & wSheet.Index
.Hyperlinks.Add Anchor:=.Range("H1"), Address:="", SubAddress:="Index", TextToDisplay:="Back to Index"
End With
Me.Hyperlinks.Add Anchor:=Me.Cells(M, 1), Address:="", SubAddress:="Start" & wSheet.Index, TextToDisplay:=wSheet.Name
End If
Next wSheet
End Sub
Last edited by a moderator: