Assign a Macro to Hyperlink

jnsturze

New Member
Joined
Nov 19, 2010
Messages
9
I a found a macro that will run when a hyperlink is selected. however, the problem is that if i have more than one hyperlink it will only work on the macro that was there first. Here is my example below:

"Private Sub Worksheet_Asset_Value(ByVal Target As Hyperlink)
'Check if the Target Address is same as you have given
'In the above example i have taken A4 Cell, so I am
'Comparing this with $A$4
If Target.Range.Address = "$A$5:$A$7" Then
Call Asset_Value
Exit Sub
End If
End Sub
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
'Check if the Target Address is same as you have given
'In the above example i have taken A4 Cell, so I am
'Comparing this with $A$4
If Target.Range.Address = "$A$8:$A$9" Then
Call Negatives
Exit Sub
End If
End Sub"

As you can see, i want to run two different macros by clicking on 2 different cells (A5:a7) and (a8:a9). any help? potentially combine the cells to call different macros depending on what cell i have selected? Thanks for the help.
 
No. All you should have to do is double click on the cell. Now you do know double click means clicking twice quickly.
and you did follow these instructions :

Right-click on the sheet tab
Select View Code from the pop-up context menu
Paste the code in the VBA edit window

This is not a module script. It does not get pasted in a module.
 
Upvote 0

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
It worked!! Oh my goodness thank you. That was exactly what I was doing wrong from the beginning.

Thanks, Yoda :)
 
Upvote 0
Now if you look at my first posting you will see how you can add a lot more of these if you want.
Glad I was able to help you. Come back here to Mr. Excel next time you need additional assistance.
 
Upvote 0
No. All you should have to do is double click on the cell. Now you do know double click means clicking twice quickly.
and you did follow these instructions :

Right-click on the sheet tab
Select View Code from the pop-up context menu
Paste the code in the VBA edit window

This is not a module script. It does not get pasted in a module.

Hey, I know this thread a bit old, but this is some great info. When I've tried to replicate this using my own macro's get an Compile Error, saying that the Sub or Function is Not Defined.

Here is the code I am using in the sheet (gotten to as per the directions above):

"Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)

Cancel = True
If Not Intersect(Target, Range("D48")) Is Nothing Then
Call ViewDashboard_Open

End If
If Not Intersect(Target, Range("F48")) Is Nothing Then
Call ViewData_Open

End If

If Not Intersect(Target, Range("I48")) Is Nothing Then
Call UnlockAll_Open

End If
If Not Intersect(Target, Range("K48")) Is Nothing Then
Call LockAll_Open

End If
End Sub"


And here's an example of the first macro as it sits in Module 4:

"
Private Sub ViewDashboard_Open()
' ViewDashboard Macro
ActiveSheet.Unprotect Password:="ABC123"
ActiveWorkbook.Unprotect Password:="ABC123"
Sheets("Entry Form").Select
Sheets("Odds and Ends").Visible = True
Sheets("Odds and Ends").Select
Range("C2").Select
End Sub
"

Do you see anything particularly wrong with what I've done?
 
Upvote 0
The error means you do not have a Macro Named that way:
To make things easy for testing just make a simple macro like

Code:
Sub Hello
Msgbox "Hello"
End Sub

And then use this simple script:

Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Application.ScreenUpdating = False
Cancel = True
If Not Intersect(Target, Range("D48")) Is Nothing Then Call Hello
'If Not Intersect(Target, Range("D56")) Is Nothing Then Call Goodbye
'If Not Intersect(Target, Range("D69")) Is Nothing Then Call Boy
'If Not Intersect(Target, Range("C45")) Is Nothing Then Call Jane
Application.ScreenUpdating = True
End Sub

'Remove this mark "'" from in front of each line of code when you want that line of code to work



Start with just one script till you see how things work
 
Last edited:
Upvote 0
The error means you do not have a Macro Named that way:
To make things easy for testing just make a simple macro like

Code:
Sub Hello
Msgbox "Hello"
End Sub

And then use this simple script:

Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Application.ScreenUpdating = False
Cancel = True
If Not Intersect(Target, Range("D48")) Is Nothing Then Call Hello
'If Not Intersect(Target, Range("D56")) Is Nothing Then Call Goodbye
'If Not Intersect(Target, Range("D69")) Is Nothing Then Call Boy
'If Not Intersect(Target, Range("C45")) Is Nothing Then Call Jane
Application.ScreenUpdating = True
End Sub

'Remove this mark "'" from in front of each line of code when you want that line of code to work



Start with just one script till you see how things work


Thank you SO much! I got it working for my many and varied macros. I had to adapt the test script you wrote above, but it works finally!
 
Upvote 0
Glad I was able to help you. Yes I knew you would need to make modifications this was just a example.
Thank you SO much! I got it working for my many and varied macros. I had to adapt the test script you wrote above, but it works finally!
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,316
Members
452,634
Latest member
cpostell

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