Macro for Hyperlink list?

Littlenia

New Member
Joined
Aug 26, 2022
Messages
4
Office Version
  1. 2010
Platform
  1. Windows
Hi everyone,

I have a macro that creates a new worksheet and then adds a hyperlink to a main index page, however it's adding the hyperlink to whatever cell I happen to have clicked into rather than into a list.

In the sheet "Index" I need a hyperlink for the newly created worksheet to be added under the last one already there. Is there a way to do this please?

The code I'm using at the minute looks like this:

VBA Code:
Sub NewRecipe()

Dim strName As String

Dim strLink As String

'get the name

    'InputBox(prompt[, title] [, default] [, xpos] [, ypos] [, helpfile, context])
    strName = InputBox("Enter Recipe Name.", "NAME COLLECTOR")
    'Exit sub if Cancel button used or no text entered
    If strName = vbNullString Then Exit Sub

    MsgBox "Creating Tab " & strName

'create new tab, rename new tab

    Sheets("Recipe Template").Copy After:=Worksheets(Worksheets.Count)
    Sheets("Recipe Template (2)").Name = strName
    Sheets("Index").Select
    Application.CutCopyMode = False

'create hyperlink to new tab

    strLink = "'" & strName & "'!A1"
    Range("A3").EntireColumn.AutoFit
    ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:= _
        strLink, TextToDisplay:=strName
    Range("A3").EntireColumn.AutoFit

End Sub

Thanks in advance!
Nia
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
This macro will add the new sheet name to the first available row in column A of Index and create the hyperlink to the new sheet.
VBA Code:
Sub NewRecipe()
    Dim strName As String, strLink As String
    strName = InputBox("Enter Recipe Name.", "NAME COLLECTOR")
    If strName = vbNullString Then Exit Sub
    MsgBox "Creating Tab " & strName
    Sheets("Recipe Template").Copy After:=Worksheets(Worksheets.Count)
    ActiveSheet.Name = strName
    With Sheets("Index")
        .Cells(.Rows.Count, "A").End(xlUp).Offset(1) = strName
        strLink = "'" & strName & "'!A1"
        .Hyperlinks.Add Anchor:=.Range("A2", .Range("A" & Rows.Count).End(xlUp)), Address:="", SubAddress:=strLink, TextToDisplay:=strName
        .Columns(1).AutoFit
    End With
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top