Automatically insert code into worksheet via macro

T

Tim879

I'd like to be able to insert the code below into the selected
worksheet when the user runs a macro. Is it possible to run a macro
that will insert this pre-written code into a worksheet?



'*******************
'paste this code into a tab. whenever it's activated it will create an
index of each
'sheet in the workbook dynamically and then also put a return link on
each tab
'*******************

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top