In Excel, can I use a hyperlink to run vba macro?
I have a spreadsheet that has many rows of data. I would like to be able to click on a cell that will run a macro using the data from that row. Since the number of rows will always be changing, I though a hyperlink for each row might be the best way.
ROW MeterID Lat Long ReadX ReadY ReadZ CoeffA CoeffB CoeffC
2 10f62gs 34.1 33.3 102.2 231.3 382.2 4.34 22.1 0.002
3 83gs72g 34.4 31.4 109.2 213.1 372.1 2.23 12.7 0.023
4 43gS128 33.3 32.2 118.8 138.7 241.8 1.94 5.08 0.107
Is there a way to run a vba macro from clicking on a hyperlink and being able to know the row of the cell that clicked on the hyperlink?
Solution 1:
This will work for you
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
MsgBox "Row " & ActiveCell.Row & " is clicked"
End Sub
Solution 2:
The more interesting way of hyperlink to run a macro, looks to be the next approach (Hyperlink formula). It does not need any event:
- Write a hyperlink formula in a cell. This can be done in VBA, too:
Sub testCreateHyperlinkFunction()
'Very important to have # in front of the function name!
Range("A1:A5").Formula = "=HYPERLINK(""#MyFunctionkClick()"", ""Run a function..."")"
End Sub
A more spectacular use will be the next approach, able to keep the initial cells value of the processed range ("A1:A5" in example):
Sub testCreateHyperlinkFunctionBis()
Dim rng As Range, arr As Variant, c As Range, i As Long
Set rng = Range("A1:A5")
arr = rng.Value
For i = 1 To UBound(arr, 1)
Range("A" & i).Formula = "=HYPERLINK(""#MyFunctionkClick()"", " & _
IIf(IsNumeric(arr(i, 1)), arr(i, 1), """" & arr(i, 1) & """") & ")"
Next i
End Sub
- Create the function to be called (in a module):
Function MyFunctionkClick()
Set MyFunctionkClick = Selection 'This is required for the link to work properly
MsgBox "The clicked cell addres is " & Selection.row
End Function
Do note the Set MyFunctionkClick = Selection line really is needed. The function needs to know, somehow, to what cell the code is referring. If this is missing, the function is called twice and you get a "Reference is invalid" error.
- Clicking the cell, the function
MyFunctionkClick()
runs...
Solution 3:
Yes you can, follow the below Simple Steps to do so:
- Select the Cell Where you want to make the Hyperlink
- Righ Click –> Hyperlink…
- Enter the Address of the Same cell where you are making the hyperlink and Give name to the Link. See the below picture:
Assign Macro to a Hyperlink
- Click Ok.
- HyperLink is created.
Note: Clicking on this Hyperlink, will do nothing because it is assigned to the same Cell Address.
- Now Press Alt + F11
- Copy paste the below Code
Run Excel Macro by Clicking on a Hyperlink
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$4" Then
'Write your all VBA Code, which you want to execute
'Or Call the function or Macro which you have
'written or recorded.
MsgBox "Write your Code here to be executed"
Exit Sub
End If
End Sub
In the Above Code we are comparing the Cell Address and then Executing a Set of Code or Function. There is another way of doing this also. We can Compare with the Target Name and execute the Code. In the above Example as i have given the Name of the Hyperlink Target as MyMacro.
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
'Check if the Target Name is same as you have given
'In the above example i have given the Name of the HyperLink
'is MyMacro.
If Target.Name = "mymacro" Then
'Write your all VBA Code, which you want to execute
'Or Call the function or Macro which you have
'written or recorded.
MsgBox "Write your Code here to be executed"
Exit Sub
End If
End Sub
Solution 4:
I'd just like to add another approach inspired by one of the answers here, that I've used in the past. It means you don't have to create hyperlinks or a helper column to initiate processing the row, you can just double click on any cell in the row you want to process:
On the sheet you want to have the double-clicking work use this:
Private Sub Worksheet_Activate()
Application.OnDoubleClick = "Module1.ProcessRow"
End Sub
Private Sub Worksheet_Deactivate()
Application.OnDoubleClick = ""
End Sub
And then in Module1
have a routine that will process the active cell:
Sub processRow()
MsgBox "Row " & ActiveCell.Row & " on " & ActiveSheet.Name & " was clicked"
End Sub
You should also disable the Excel double-click method in the workbook events:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.OnDoubleClick = ""
End Sub
I find the double-click method is really, really handy and intuitive for a lot of processes e.g. bringing up a UserForm with data populated from the row or transferring that data to another sheet maybe with calcs or formatting etc.
Solution 5:
I think rather than go through the hassle of creating a hyperlink for each cell, you would be better off creating a macro that references the Activecell
property. Next, create a keyboard shortcut for the macro to run. To do so:
- Press ALT+F8
- Select
Options
- Choose a key to be the shortcut key
If you already have hyperlinks, triggering a macro using the Hyperlink_Follow
event may be best. If not, then consider my recommendation.