Showing posts with label hyperlink. Show all posts
Showing posts with label hyperlink. Show all posts

Monday, March 14, 2016

How to run a macro from an hyperlink event EXCEL VBA

Follow these steps to run a macro from an hyperlink:

First, create manually an hyperlink which is linked to its own cell in EXCEL:
Note you can create hyperlinks automatically using a macro: see post http://domyexcelblog.blogspot.com/2016/03/how-to-add-hyperlinks-for-macro-call.html



Then, create the Hyperlink event in VBA:


Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)

MsgBox ("You just clicked on the hyperlink on cell column = " & ActiveCell.Column & ", row = " & ActiveCell.Row)

End Sub


Finally, test your macro: when you click on the hyperlink the following message from your macro will appear:



Created by DoMyExcel.com © | Excel & VBA Consulting | Customized Excel Solutions starting at 89 USD
Services provided to Individuals and Businesses | Free Quote: DoMyExcel@gmail.com

How to add hyperlinks for macro call outs in EXCEL VBA

This program will create hyperlinks that link to their own cell in excel, it is then easy to use these hyperlinks to create macros (see blog post http://domyexcelblog.blogspot.com/2016/03/how-to-run-macro-from-hyperlink-event.html ) :

Sub Add_Hyperlinks_for_macros()

For i = 6 To 200
    If Cells(i, 5) <> "" Then
                ActiveSheet.Cells(i, 5).Select
                ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:="", SubAddress:= _
                    "'My Contacts'!e" & i, TextToDisplay:="Add Photo"
    End If
Next

End Sub




Created by DoMyExcel.com © | Excel & VBA Consulting | Customized Excel Solutions starting at 29 USD
Services provided to Individuals and Businesses | Free Quote: DoMyExcel@gmail.com