PDA

View Full Version : [SOLVED:] VBA Hyperlink not working



Aussiebear
07-07-2023, 05:34 AM
In the attached workbook, I have an example of manually creating a working hyperlink by the following formula in cell C1

=hyperlink(B1,A1)

However if I try to recreate this by using a Sub, it fails with the following code Error Msg: Requires a separator and highlights the second line



Sub InsertHyperlinkInCell()
ActiveWorkbook.Worksheets("Sheet1").Range(C2).Formula = "=hyperlink(B2,A2)"
End Sub


I can't see where it would require one.

Paul_Hossler
07-07-2023, 05:50 AM
Works here -- Win 10 64 bit

30920


However ... the way I usually add a hyperlink



Option Explicit


Sub PHH_Macro()
With ActiveSheet
.Range("C2").Hyperlinks.Add Anchor:=.Range("C2"), Address:="", SubAddress:="Sheet1!B1", TextToDisplay:=.Range("A2").Value
End With
End Sub

June7
07-07-2023, 06:03 AM
Testing code in my workbook, I got error message "variable not defined" on the C2 reference. Enclosing in quotes works.

ActiveWorkbook.Worksheets("Sheet1").Range("C2").Formula = "=hyperlink(B2,A2)"

Paul_Hossler
07-07-2023, 06:08 AM
Enclosing in quotes works.

Missed that:crying:

but the attachment was correct and that seems to work for me


30921

Aussiebear
07-07-2023, 06:55 AM
Don't know what happened here but it's working as intended (as initially written). Thank you Paul for your version.