I am trying to create VBA code that identifies named ranges in my workbook and creates a hyperlink for each. These hyperlinks need to be capable of being copied to other documents.
I found two examples--each one does half of the job, but am having trouble combining them:
1) Find and list ranges:
Sub namedranges()
For Each n In ThisWorkbook.Names
i = i + 1
Range("a" & i + 1) = n.Name
Next n
End Sub
2) Create Hyperlinks of all worksheets:
Sub CreateLinksToAllSheets()
Dim sh As Worksheet
Dim cell As Range
For Each sh In ActiveWorkbook.Worksheets
If ActiveSheet.Name <> sh.Name Then
ActiveCell.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:="'" & sh.Name & "'" & "!A1", TextToDisplay:=sh.Name
ActiveCell.Offset(1, 0).Select
End If
Next sh
End Sub
When I combine, as follows, I get an error that object/variable not set.
Sub hyperlinknamedranges()
Dim sh as worksheet
Dim cell as range
For Each n in Thisworkbook.names
' ActiveCell.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:="'" & sh.Name & "'" & "#" & "'" & n.name & "'",
ActiveCell.Offset(1, 0).Select
next n
End Sub
The final code does not need to produce a listing of ranges, as is done in the first example.