PDA

View Full Version : Linking macros to hyperlinks in VB



Frantheman
02-14-2013, 12:41 AM
I have successfully managed to link a pre-recorded macro (PrintWCBase) to a hyperlink situated in a specific cell (K20) on a worksheet with the following code:

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
If Target.Range.Address = "$K$20" Then
Run ("PrintWCBase")
Exit Sub
End If
End Sub

However I want to link a different macro (InfoWCBase) to a hyperlink in another cell (K22) on the same worksheet and I am really struggeling.
Please can you help me???

Aflatoon
02-14-2013, 02:30 AM
You would need something like this
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
If Target.Range.Address = "$K$20" Then
Call PrintWCBase
ElseIf Target.Range.Address = "$K$22" Then
Call InfoWCBase
End If
End Sub

Frantheman
02-14-2013, 03:37 AM
:help Thanks Aflatoon
One more thing though:

How do I go about adding more macros to more hyperlinks?

Clearly this way is wrong:

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
If Target.Range.Address = "$K$20" Then
Call PrintWCBase
ElseIf Target.Range.Address = "$K$22" Then
Call InfoWCBase
ElseIf Target.Range.Address = "$K$59" Then
Call PrintWCHurry
End If
End Sub

Aflatoon
02-14-2013, 06:39 AM
Why do you say that is wrong?

Frantheman
02-14-2013, 08:14 AM
I took the code you supplied me and it worked perfectly for the two hyperlinks as supplied. As soon as I added the additional macro the third one does not respond. I have to do this for multiple (100 +) macros and links. Help appreciated.

Jan Karel Pieterse
02-14-2013, 08:37 AM
WHat happens if you step through the code? (select the Private sub... line and press the F9 key. Then click anoffending link)

Aflatoon
02-14-2013, 08:48 AM
The syntax is correct. Is it a manual hyperlink or the result of a HYPERLINK formula?

Kenneth Hobs
02-14-2013, 09:55 AM
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
Select Case Target.Range.Address(False, False)
Case "A1"
Macro1
Case "A3"
Macro3
Case "A5"
Macro5
Case Else
MsgBox "You clicked the hyperlink in: " & Target.Range.Address(False, False)
End Select
End Sub

Frantheman
02-15-2013, 02:39 AM
Dear Kenneth
This works like a charm!!!
Thank you so much.:joy: