use/ reference VBA Worksheet event in module for use in all workbooks

stevewingjr

New Member
Joined
Apr 24, 2014
Messages
7
Hi there, I'm very new to VBA, first of all, so thank in advance for patience.

I have this code saved to "ThisWorkbook" on my PERSONAL workbook:

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
MsgBox "Name of Sheet : " & Sh.Name
End Sub

it works fine when I'm within the PERSONAL workbook itself, but doesn't seem to do anything when I change sheets in a different workbook (since it's a workbook-specific event).

Is there any way to change this to an application event to run whenever I activate (more specifically, switch between) any worksheet in any open workbook in excel? or call/run this event in a module (this doesn't necesarily have to launch when excel opens, I'm fine with launching a macro to start looking for this event)?

Just changing "Private Sub" to "Public Sub" doesn't really seem to do anything

MsgBox "Name of Sheet :"& Sh.Name
is just a space holder, ultimately the macro will check if the active sheet is connected to a database and return what certain database preferences have been set to (unique to that worksheet's connection). As I can have multiple worksheets connected to multiple instances of databases, I plan on using this as a reminder of what the preferences of each connection are when I open them.

I've thought about using application.ontime, but I don't need it to run THAT regularly, and the information is really database worksheet specific.

Maybe I'm not thinking about this correctly, so maybe there's a better way to do this. Any help would be appreciated. Thanks!
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Very helpful! Thanks for the link to Chip Pearson's page too. That was absolutely the missing link.

Much appreciated.
 
Upvote 0

Forum statistics

Threads
1,212,929
Messages
6,110,740
Members
448,295
Latest member
Uzair Tahir Khan

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