ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Automatically insert code into worksheet via macro (https://www.excelbanter.com/excel-programming/412521-automatically-insert-code-into-worksheet-via-macro.html)

Tim879

Automatically insert code into worksheet via macro
 
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








All times are GMT +1. The time now is 05:33 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com