Run macro with hyperlink, from add-in

Formula11

Active Member
Joined
Mar 1, 2005
Messages
461
Office Version
  1. 365
Platform
  1. Windows
To run a macro from a hyperlink, code below is used.
Cell with hyperlink is named, then right-click, Link, Place in This Document, choose named cell.

It works in workbook with code, why doesn't it work from add-in?

VBA Code:
'CODE IN WORKSHEET
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
    If Target.Name = Range("named_cell") Then
        MsgBox "Clicked"
    End If
End Sub
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
You'll need to use the application-level event handler...

1) Insert a class module (Visual Basic Editor >> Insert >> Class Module).

2) Name the class module clsApp in the Properties window under Name.

3) Place the following code in the class module...

VBA Code:
Option Explicit

Public WithEvents xl As Excel.Application

Private Sub xl_SheetFollowHyperlink(ByVal Sh As Object, ByVal Target As Hyperlink)
    
    'Your code here
    '
    '
    
End Sub

4) Insert a regular module (Visual Basic Editor >> Insert >> Module).

5) Place the following in the regular module...

VBA Code:
Option Explicit

Dim xlApp As clsApp

Sub Init()
    
    Set xlApp = New clsApp
    Set xlApp.xl = Application
    
End Sub

6) In the Project Explorer window, right-click ThisWorkbook, select View Code, and place the following code in the module...

VBA Code:
Option Explicit

Private Sub Workbook_Open()
    Init
End Sub

Hope this helps!
 
Upvote 0
Solution
Thanks Domenic, instructions worked, and very detailed, cheers
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,774
Members
452,353
Latest member
strainu

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