How to Assign a Macro – to a hyperlink in Excel?
Did you just thought a button click or keyboard shortcut – are the only possible way.
Run macro using hyperlink !!! – learn in just 2 mins.
oh. well. Where do we start? Lets go one by one. But don’t skip any step.
Each one is important.
- First create a Hyperlink in worksheet.
- A macro to run with hyperlink click.
- Map Hyperlink with Macro
1. Create Hyperlink for Macro
I will explain with a simple example. Enter “Run macro using Hyperlink” in cell A1 of a Excel worksheet.
Lets make it a hyperlink.
- Right click on cell A1. Choose Hyperlink.
- Choose “Place in this document” in Pop-up window.
- Enter A1 in “Type the cell reference”
- Click Ok
Hyperlink created. Rush to next step.
2. Macro to run with above Hyperlink
Create a simple macro to map with above href.
Any simple macro will do. Lets not waste time here.
Sub Hello_World_Hyperlink_Macro() 'Just a Hello world program MsgBox "Yes !!! I ddi it. This macro just ran by clicking a hyperlink in cell" End Sub
Just a simple “Hyperlink macro hello world program”.
3. Assign Macro to Hyperlink
Open the sheet that has the Hyperlink. Press Alt + F11 to open vbe editor.
Double sheet1 in vbe. Then copy the below code & paste it in vbe. Thats all what is require.
'Macro to Run Another macro based on Hyperlink Click Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink) 'Get Address specified in Hyperlink Mapping Select Case Target.Range.Address 'Check for Address & run corresponding macro Case "$A$1" Call Hello_World_Hyperlink_Macro End Select End Sub
All set. go to the sheet1.
Just click on the text in cell A1 & see the magic.
This will run the above macro assigned to the hyperlink.
Not only one macro, We can just map any number of macro with list of hyperlinks using this one code sample. All you need to refer is the range.address of the cell & call corresponding macro code for that href.
Please leave a comment if you liked this topic.