VBA add hyperlink to specific sheet - reference isn't valid

mysticmario

Active Member
Joined
Nov 10, 2021
Messages
323
Office Version
  1. 365
Platform
  1. Windows
Hi I tried multiple versions of this code to create a proper hyperlink to another sheet in the same workbook:
VBA Code:
lr = Sheets("PROJEKTY ZAKOŃCZONE").Cells(Rows.Count, "F").End(xlUp).Row
Dim wb As Workbook
Set wb = ThisWorkbook
Dim adressn As String
adressn = "Godziny" + Left(ans2, 5)
Dim ws As Worksheet
Set ws = wb.Worksheets(adressn)
Sheets("PROJEKTY ZAKOŃCZONE").Cells(lr + 1, "F").Hyperlinks.add Anchor:=Sheets("PROJEKTY ZAKOŃCZONE").Cells(lr + 1, "F"), Address:="", SubAddress:="ws", TextToDisplay:="Open"

The code works to a point where it shoudl assign address to go to
1643890054654.png


When u click the link all I get is:
1643890093290.png


I'm loosing my mind with this one
variable seen as ans2 is defined earlier in the code and works fine. In the picture u can see debug printed sheet name in cell F41 that is the exact name of the sheet I'm trying to go to.
Can anyone help?
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
The SubAddress needs a sheet name and a cell reference (e.g., "Godziny03026!A1"). Since adressn is the name, you don't need to use ws in the hyperlink. If you don't use ws later in the code (it was only for the hyperlink), you can get rid of it.

If A1 is not your preferred link cell, you can change it.
VBA Code:
lr = Sheets("PROJEKTY ZAKOŃCZONE").Cells(Rows.Count, "F").End(xlUp).Row
Dim wb As Workbook
Set wb = ThisWorkbook
Dim adressn As String
adressn = "Godziny" + Left(ans2, 5)
Dim ws As Worksheet
Set ws = wb.Worksheets(adressn)
Sheets("PROJEKTY ZAKOŃCZONE").Cells(lr + 1, "F").Hyperlinks.add Anchor:=Sheets("PROJEKTY ZAKOŃCZONE").Cells(lr + 1, "F"), Address:="", SubAddress:=adressn & "!A1", TextToDisplay:="Open"
 
Upvote 0
Solution
The SubAddress needs a sheet name and a cell reference (e.g., "Godziny03026!A1"). Since adressn is the name, you don't need to use ws in the hyperlink. If you don't use ws later in the code (it was only for the hyperlink), you can get rid of it.

If A1 is not your preferred link cell, you can change it.
VBA Code:
lr = Sheets("PROJEKTY ZAKOŃCZONE").Cells(Rows.Count, "F").End(xlUp).Row
Dim wb As Workbook
Set wb = ThisWorkbook
Dim adressn As String
adressn = "Godziny" + Left(ans2, 5)
Dim ws As Worksheet
Set ws = wb.Worksheets(adressn)
Sheets("PROJEKTY ZAKOŃCZONE").Cells(lr + 1, "F").Hyperlinks.add Anchor:=Sheets("PROJEKTY ZAKOŃCZONE").Cells(lr + 1, "F"), Address:="", SubAddress:=adressn & "!A1", TextToDisplay:="Open"
thank you, of course i tried the same thign with a wrong syntax
i had subaddress:="adressn!A1"
Thank you again
that fixed the problem instantly
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,964
Members
452,371
Latest member
Frana

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