Hyperlinks Subaddress = variable worksheet (VBA)

bukimi

Board Regular
Joined
Apr 12, 2017
Messages
105
Office Version
  1. 2019
Platform
  1. Windows
Hello!

I'm struggling to get it right, but whatever I try, I can't make it work.

I have a table in which I need to make every cell in first column as a link to a worksheet of the same name that is inside the cell.
(that is: cells in 1st column is 1:1 a list of worksheets, excluding worksheet #1 which is a table of contents named "Zestawienie" with some details)

I tried this one:
Code:
Dim wZestawieniu As Integer
Dim ArkuszDocelowy As Range


wZestawieniu = 2


Do Until Cells(wZestawieniu, 1).Value = ""
Set ArkuszDocelowy = Worksheets(wZestawieniu).Cells(1, 1)
Sheets("ZESTAWIENIE").Hyperlinks.Add Anchor:=Cells(wZestawieniu, 1), Address:="", SubAddress:=ArkuszDocelowy
wZestawieniu = wZestawieniu + 1
Loop
but get error when hyperlink is about to be added.

This one doesn't work either:
Code:
Dim WS As Worksheet
Dim wZestawieniu As Integer
Dim ArkuszDocelowy As Range

For Each WS In ActiveWorkbook.Worksheets
Sheets("ZESTAWIENIE").Hyperlinks.Add Anchor:=Cells(wZestawieniu, 1), Address:="", SubAddress:=Chr(34) & Chr(39) & WS.Name & Chr(39) & "A1" & Chr(34)
wZestawieniu = wZestawieniu + 1
Next

I don't know how to put variable WorkSheet name as subaddress for hyperlink inside a cell.
As far as I know Anchor tells VBA which cell will be an active link, Address is required only if it's external link (empty if not) and Subaddress is a name of target worksheet and range (presumably A1).

This code recorded by macro recorder obviously works, but it has constant worksheet name here (BodyFit), which I need to be a variable:
Code:
Range("A3").Select
    ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:= _
        "'Bodyfit'!A1"

Thank you in advance for all your help!
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
So you have a table that has sheet names in its first column and you want to convert those cells to be hyperlinks to cell A1 of the sheet name?
 
Upvote 0
So you have a table that has sheet names in its first column and you want to convert those cells to be hyperlinks to cell A1 of the sheet name?

Yes, but without changing the text inside that cell. Final result should be a clickable sheet name within a cell (by default hightlighted in blue) that directs user to that sheet (cell A1 or wherever in it).
 
Upvote 0
Something like this perhaps:

Code:
Dim sh As Worksheet, ws As Worksheet
Dim lr As Long, a As Long
Dim arr As Variant
Dim c As Range

Set sh = Sheets("Table") 'change to suit
lr = sh.Range("A" & Rows.Count).End(xlUp).Row

ReDim arr(0 To Worksheets.Count - 1)
For Each ws In ThisWorkbook.Worksheets
    arr(a) = ws.Name
    a = a + 1
Next

For Each c In sh.Range("A1:A" & lr) 'change to suit
    If Len(c.Value) > 0 Then
        myMatch = Application.Match(c.Value, arr, 0)
        If Not IsError(myMatch) Then
            sh.Hyperlinks.Add Anchor:=c, Address:="", SubAddress:="'" & c.Value & "'!A1"
        End If
    End If
Next
 
Upvote 0
That line
Code:
[COLOR=#333333]sh.Hyperlinks.Add Anchor:=c, Address:="", SubAddress:="'" & c.Value & "'!A1"[/COLOR]
gave me exactly what I needed. I couldn't grasp how to add these ' symbols and !A1 and put worksheetname from variable there too.

Finally my working code looks like that:
Code:
Dim wZestawieniu As Integer


wZestawieniu = 2


Do Until Cells(wZestawieniu, 1).Value = ""
Sheets("ZESTAWIENIE").Hyperlinks.Add Anchor:=Cells(wZestawieniu, 1), Address:="", SubAddress:="'" & Worksheets(wZestawieniu).Name & "'!A1"
wZestawieniu = wZestawieniu + 1
Loop
I didn't mention that worksheets are in the same order in the file and on the list, so simple match by worksheet number in order is enough. I dropped that name matching part for cases where someone would make a small mistake in the table or worksheet name (no match then).
"Do until" empty value also is enough to stop code when list is over :)

Thank you very much. You helped me a lot!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,217
Members
453,024
Latest member
Wingit77

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