Reference isn't valid - VBA generated hyperlink

mysticmario

Active Member
Joined
Nov 10, 2021
Messages
323
Office Version
  1. 365
Platform
  1. Windows
Hi,
I used this code previously on with anoterh application, when i tried to replicate this for new usage I get the reference isnt valid error and for the love of god I cannot get it to work
here's the code:
VBA Code:
Dim Ws As Worksheet, lrQ As Long
Dim wb As Workbook
Set wb = ThisWorkbook
Dim adressn As String

    For Each Ws In Worksheets
        If InStr(Ws.Name, "Witold_") Then
        adressn = Ws.Name
        Set Ws = wb.Worksheets(adressn)
        lrQ = 6
            Sheets("PANEL WYCEN").Cells(lrQ + 1, "Q").Hyperlinks.add Anchor:=Sheets("PANEL WYCEN").Cells(lrQ + 1, "Q"), Address:="", SubAddress:=adressn & "!A1", TextToDisplay:=adressn
            lr = lr + 1
        End If
    Next Ws
Any Idea why this happens?
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Some "Witold_" worksheets should have space in the name. That's the reason of the error when you click on the generated link.
Use the following code line instead. Note that I wrapped the addresn with single quotes to make it work correctly.

VBA Code:
Sheets("PANEL WYCEN").Cells(lrQ + 1, "Q").Hyperlinks.Add Anchor:=Sheets("PANEL WYCEN").Cells(lrQ + 1, "Q"), Address:="", SubAddress:="'" & adressn & "'!A1", TextToDisplay:=adressn

Also, although these are not causing any errors:
  • The lr variable name should be lrQ after that line.
  • lrQ = 6 line above that line should be moved out of the loop (just before the loop is fine). Otherwise, all links will be created in cell Q6 since lrQ will be always assigned as 6 in the loop.
Something like this after all suggested changes. I suggest using Option Explicit to avoid such declaration mistakes.
VBA Code:
Option Explicit

Sub MacroName()
Dim Ws As Worksheet, lrQ As Long
Dim wb As Workbook
Set wb = ThisWorkbook
Dim adressn As String
    lrQ = 6
    For Each Ws In Worksheets
        If InStr(Ws.Name, "Witold_") Then
        adressn = Ws.Name
        Set Ws = wb.Worksheets(adressn)
            Sheets("PANEL WYCEN").Cells(lrQ + 1, "Q").Hyperlinks.Add Anchor:=Sheets("PANEL WYCEN").Cells(lrQ + 1, "Q"), Address:="", SubAddress:="'" & adressn & "'!A1", TextToDisplay:=adressn
            lrQ = lrQ + 1
        End If
    Next Ws
End Sub
 
Upvote 0
Solution
Some "Witold_" worksheets should have space in the name. That's the reason of the error when you click on the generated link.
Use the following code line instead. Note that I wrapped the addresn with single quotes to make it work correctly.

VBA Code:
Sheets("PANEL WYCEN").Cells(lrQ + 1, "Q").Hyperlinks.Add Anchor:=Sheets("PANEL WYCEN").Cells(lrQ + 1, "Q"), Address:="", SubAddress:="'" & adressn & "'!A1", TextToDisplay:=adressn

Also, although these are not causing any errors:
  • The lr variable name should be lrQ after that line.
  • lrQ = 6 line above that line should be moved out of the loop (just before the loop is fine). Otherwise, all links will be created in cell Q6 since lrQ will be always assigned as 6 in the loop.
Something like this after all suggested changes. I suggest using Option Explicit to avoid such declaration mistakes.
VBA Code:
Option Explicit

Sub MacroName()
Dim Ws As Worksheet, lrQ As Long
Dim wb As Workbook
Set wb = ThisWorkbook
Dim adressn As String
    lrQ = 6
    For Each Ws In Worksheets
        If InStr(Ws.Name, "Witold_") Then
        adressn = Ws.Name
        Set Ws = wb.Worksheets(adressn)
            Sheets("PANEL WYCEN").Cells(lrQ + 1, "Q").Hyperlinks.Add Anchor:=Sheets("PANEL WYCEN").Cells(lrQ + 1, "Q"), Address:="", SubAddress:="'" & adressn & "'!A1", TextToDisplay:=adressn
            lrQ = lrQ + 1
        End If
    Next Ws
End Sub
Than you the codefor hyperlinks now works great.
As for the later part, I've noticed that aswell and I fixed that soon after posting the issue.
Thank you for your help(y)
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,636
Latest member
laura12345

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