So far we have always seen running any Macro or Function using a Command Button. Sometimes, instead of putting a Command Button, you want to do the same operations but by using a Hyperlink in Excel. In this article you are going to learn how to run a macro by using Hyperlink in Excel.
 
Follow the below Simple Steps to do so:

Step 1. Select the Cell Where you want to make the Hyperlink
Step 2. Righ Click –> Hyperlink…
Step 3. 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

Assign Macro to a Hyperlink



Step 4. Click Ok.
Step 5. HyperLink is created.
 
Note: Clicking on this Hyperlink, will do nothing because it is assigned to the same Cell Address.
 
Step 6. Now Press Alt + F11
Step 7. Copy paste the below Code as shown in Picture

Run Excel Macro by Clicking on a Hyperlink

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

 
To Check out Excel Macro Tutorials, visit Excel Macro Tutorial

cover3d_0-89071700_1484285537__1_

Join over 10, 000+ Excel VBA Enthusiasts & get this FREE e-Book Now!