Make SheetSelectionChange operate on a different workbook

stevenjwilson

New Member
Joined
Jan 3, 2013
Messages
1
I use Workbook_SheetSelectionChange in the "ThisWorkbook" object of a excel file, file 1. I have Module code in this file that I make available as commands on the Excel ribbon.

The problem I have is that I want the SheetSelectionChange event to monitor a different workbook, file 2, but of course it doesn't .... its only active on file 1. The scenario is this: file 1 is closed; I open file 2, the commands are on the Excel ribbon, I can run those OK and they operation on the current workbook that is open, but of course I get no Workbook_SheetSelectionChange functionality because file 2 has no macros in it.

I have been trying to figure out options:
1) I can add the macros to file 2, but I dont want to do this. I want them to be macro free.
2) I have been reading more about how "events" work to see if there is a way to make an event monitor in file 1, that can monitor events in another workbook. I have been reading about Class Modules and wonder if that is an answer.....

In Pseudo code: instead of
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
I want
Private Sub Workbook_SheetSelectionChange(wb As Object, ByVal Sh As Object, ByVal Target As Range)
so I can tell it what workbook to monitor.

Any tips would be appreciated....Thanks
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
You'll need to use a class module to enable application level events...

1) Create a class module (Atl+F11 > Insert > Class Module).

2) In the Properties window (F4) under Name, set the name to clsApp.

3) In the code module for the class...

Code:
Option Explicit

Public WithEvents XL As Application

Private Sub XL_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    MsgBox Sh.Parent.Name & ", " & Sh.Name & ", " & Target.Address
End Sub

4) In the code module for ThisWorkbook...

Code:
[COLOR=darkblue]Option[/COLOR] [COLOR=darkblue]Explicit[/COLOR]

[COLOR=darkblue]Dim[/COLOR] xlApp [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]New[/COLOR] clsApp

[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] Workbook_BeforeClose(Cancel [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Boolean[/COLOR])
    [COLOR=darkblue]Set[/COLOR] xlApp.XL = [COLOR=darkblue]Nothing[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]

[COLOR=darkblue]Private[/COLOR] [COLOR=darkblue]Sub[/COLOR] Workbook_Open()
    [COLOR=darkblue]Set[/COLOR] xlApp.XL = Application
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]Sub[/COLOR]

5) Close and re-open the workbook.

Note that other event handlers will also be available in the class module.

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,214,651
Messages
6,120,744
Members
448,989
Latest member
mariah3

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