Hyperlink Macro - alphabetical ordering?

Littlenia

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

Apologies if this is a stupid question, I am rather new to the world of macros and sort of winging it!

I have a macro for creating a new sheet and adding a hyperlink for that sheet on an index page which is working, but ideally I'd like my hyperlinks to always be in alphabetical order. I can of course do it manually but I'm hoping there is a more clever way to achieve this!

The code I'm currently using is below, is there any way to add something to this so that as each hyperlink is added to my "index" sheet, the list of hyperlinks on that sheet are then sorted alphabetically?

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
    Sheets("Index").Select
    With Sheets("Index")
        .Cells(.Rows.Count, "A").End(xlUp).Offset(1) = strName
        strLink = "'" & strName & "'!A1"
        .Hyperlinks.Add Anchor:=.Range("A" & Rows.Count).End(xlUp), Address:="", SubAddress:=strLink, TextToDisplay:=strName
        .Columns(1).AutoFit
    End With
End Sub

Thanks in advance!
Nia
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Try adding this line

Rich (BB code):
    With Sheets("Index")
        .Cells(.Rows.Count, "A").End(xlUp).Offset(1) = strName
        strLink = "'" & strName & "'!A1"
        .Hyperlinks.Add Anchor:=.Range("A" & Rows.Count).End(xlUp), Address:="", SubAddress:=strLink, TextToDisplay:=strName
        .Columns(1).AutoFit
        .Columns(1).Sort Key1:=.Range("A2"), Order1:=xlAscending, Header:=xlYes
    End With
End Sub
 
Upvote 0
Solution
Try adding this line

Rich (BB code):
    With Sheets("Index")
        .Cells(.Rows.Count, "A").End(xlUp).Offset(1) = strName
        strLink = "'" & strName & "'!A1"
        .Hyperlinks.Add Anchor:=.Range("A" & Rows.Count).End(xlUp), Address:="", SubAddress:=strLink, TextToDisplay:=strName
        .Columns(1).AutoFit
        .Columns(1).Sort Key1:=.Range("A2"), Order1:=xlAscending, Header:=xlYes
    End With
End Sub
Perfect! Thank you so much!
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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