Catch an event in another book

I was wondering if it is possible to catch the event in another book.

The macro will open the workbook for the user and I want to be able to run another macro on the selection_change event. No, the code cannot be saved in the book you just opened.

Is there any other way than to programmatically add code to an open workbook ? ThisWorkbook

So it is insecure and unstable and usually bleh.

+3


source to share


1 answer


All you have to do is grab the link WithEvents

to the open book in the class module with event handlers. For example:



'In ThisWorkbook 
Option Explicit

Private WithEvents other As Worksheet

Private Sub Example()

    Dim wb As Workbook
    Set wb = Workbooks.Open("C:\Dev\other.xlsx")
    Set other = wb.Sheets("Sheet1")

End Sub

Private Sub other_SelectionChange(ByVal Target As Range)

    Debug.Print Target.Address

End Sub

      

+1


source







All Articles