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.
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Just combine your code like this:

Code:
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$5:$A$7" Then
        Call Asset_Value
    ElseIf Target.Range.Address = "$A$8:$A$9" Then
        Call Negatives
    End If
End Sub
 
Upvote 0
Can someone help me run this Macro from a Hyperlink in cell H3? And I just want to confirm that the hyperlink will reference cell H3. Thanks!

Sub apply_autofilter_across_worksheets()
Dim p As Integer, q As Integer
p = Worksheets.Count
For q = 1 To p
With Worksheets(q)
.Range("A1").AutoFilter Field:=1, Criteria1:=Range("'Search'!B1").Value
End With
Next q
End Sub
 
Upvote 0
Not sure why you want to assign scripts to Hyperlinks.
You can assign scripts to any cell you like by using the below type Sheet event.
Double click on the assigned cell and it will call your module script.
You can assign dozens of scripts to different cells. In the cell type in the script name or some other text that will remind you what script is assigned to that cell.

To install this code:

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

Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    
Cancel = True

    If Not Intersect(Target, Range("D3")) Is Nothing Then
         Call Delete_Shows
         
    End If
    
    If Not Intersect(Target, Range("D4")) Is Nothing Then
         Call Sort_Me
         
    End If
    
    If Not Intersect(Target, Range("D5")) Is Nothing Then
         Call Delete_Less_Then_All
         
    End If
    
If Not Intersect(Target, Range("D6")) Is Nothing Then
         Call Activate_Sheet_One
         
End If

End Sub
 
Upvote 0
Thanks. That sounds easier. I apologize, but I'm new to this, But do I insert my existing Macro into your script?
 
Upvote 0
To show you how to do this exactly tell me.
What is the name of your script?
And what cell do you want to assign the script to?
 
Upvote 0
Here is the script that I would like assigned to H3

Sub apply_autofilter_across_worksheets()
Dim p As Integer, q As Integer
p = Worksheets.Count
For q = 1 To p
With Worksheets(q)
.Range("A1").AutoFilter Field:=1, Criteria1:=Range("'Search'!B1").Value
End With
Next q
End Sub
 
Upvote 0
Here is the script: Install it as I mentioned in my earlier post.
Now when you double click on cell "H3" your script will run.

Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    
Cancel = True

    If Not Intersect(Target, Range("H3")) Is Nothing Then
         Call apply_autofilter_across_worksheets
         
    End If
    End Sub
 
Upvote 0
Do I need to push CTRL while double clicking or something like that? when I double click the cell, it just inserts the curser for editing as usual.
 
Upvote 0

Forum statistics

Threads
1,223,901
Messages
6,175,277
Members
452,629
Latest member
SahilPolekar

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