Syntax Question

stuartgb100

Active Member
Joined
May 10, 2015
Messages
322
Office Version
  1. 2021
Platform
  1. Windows
The following code snippet works well:
Sheets("Index").Range("B2").Hyperlinks.Add _
Anchor:=Sheets("Index").Range("B2"), Address:="", _
SubAddress:="'Artists 1'!B2", _
TextToDisplay:=Sheets("Artists 1").Range("B2").Value

However, when I try to use variables, it does not work:
Sheets("Index").Range("B" & TargetRw).Hyperlinks.Add _
Anchor:=Sheets("Index").Range("B" & TargetRw), Address:="", _
SubAddress:="'" & SourceSht.Name & "'" & .Range("B" & StartRw), _
TextToDisplay:=Sheets(SourceSht).Range("B" & StartRw).Value

I think there's a Type Mismatch in the last but 1 line.
Any help would be much appreciated.
Thanks.
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Are you getting some sort of error message?
What is the exact number and message of that error?

Have you verified the value of all your variables, to make sure that they are returning the values you expect, i.e.
- TargetRw
- SourceSht
- StartRw

You haven't posted the section of your code where these values are being populated, so we cannot determine if you may have made some sort of error there.
 
Upvote 0
Try this:
VBA Code:
    With SourceSht
        Sheets("Index").Range("B" & TargetRw).Hyperlinks.Add _
            Anchor:=Sheets("Index").Range("B" & TargetRw), Address:="", _
            SubAddress:="'" & .Name & "'!" & .Range("B" & StartRw).Address, _
            TextToDisplay:=SourceSht.Range("B" & StartRw).Value
    End With
 
Upvote 0
Thanks Alex,
However, this returns Run-Time Error 1004
Application-defined or object-defined error
.
 
Upvote 0
Thanks Joe4,
Apologies for the incomplete information.
The Error Message is Run-Time Error 1004
Application-defined or object-defined error
The (edited) section of code is ............

Sub CreateSub_MenuOfHyperlinksToAllWorksheets()
Dim objSheet As Worksheet, SourceSht As Worksheet
Dim TargetSht As Worksheet, TargetRw As Long
Dim StartRw As Long, EndRw As Long

Set TargetSht = Sheets("Index")

For Each objSheet In ActiveWorkbook.Worksheets
If objSheet.Name <> "Index" Then
Set SourceSht = objSheet
TargetRw = 1
With SourceSht
Sheets("Index").Range("B" & TargetRw).Hyperlinks.Add _
Anchor:=Sheets("Index").Range("B" & TargetRw), Address:="", _
SubAddress:="'" & SourceSht.Name & "'" & .Range("B" & StartRw), _
TextToDisplay:=Sheets(SourceSht).Range("B" & StartRw).Value
End With
End If
Next
End Sub
 
Upvote 0
There is your problem right there!
You are not setting "StartRw" to anything, so its value is zero, and cell "B0" is not a valid cell!
 
Upvote 0
The item @Joe4 picked up is certainly fatal.
There are more issues though.
1) Please use the code buttons, I find the VBA button the easiest.
2) You are not incrementing the target row
Rich (BB code):
TargetRw = 1
TargetRw = TargetRw + 1
3) You did not use the code I gave you, replace the code below with what I gave you in post#3
Rich (BB code):
With SourceSht
           Sheets("Index").Range("B" & TargetRw).Hyperlinks.Add _
               Anchor:=Sheets("Index").Range("B" & TargetRw), Address:="", _
               SubAddress:="'" & SourceSht.Name & "'" & .Range("B" & StartRw), _
               TextToDisplay:=Sheets(SourceSht).Range("B" & StartRw).Value
End With

Here is post#3 again modified to use TargetSht
Rich (BB code):
    With SourceSht
        TargetSht).Range("B" & TargetRw).Hyperlinks.Add _
            Anchor:=TargetSht.Range("B" & TargetRw), Address:="", _
            SubAddress:="'" & .Name & "'!" & .Range("B" & StartRw).Address, _
            TextToDisplay:=SourceSht.Range("B" & StartRw).Value
    End With
 
Upvote 0
Solution
Sorry, hit 'Post Reply' too early <doh>
Variables are declared as follows ( at the point of running the Hyperlink line:
objSheet Worksheet/Sheet1
SourceSht Worksheet/Sheet1
TargetSht Worksheet/Sheet8
TatgetRw 1 as Long
StartRw 0 as Long
EndRw 0 as Long
 
Upvote 0
The issue isn't the variable declaration!

The issue I mentioned is that nowhere in your code are you setting StartRw to anything!
So, it would default to a value of 0. And you cannot have a row with number 0. That is invalid and result in an error!
 
Upvote 0
Joe4 & Alex,
Please ignore my last few posts.
Sincere apologies for wasting your time.
I did have the variables correctly defined, but did not post the correct code snippet
Must be getting too old for this !
Alex, that worked fine, thanks.

I dip in and out of vba, sometimes with a year or more between episodes.
I forget so much - even the simple things !

Very many thanks both, for bearing with me.
Cheers.
 
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