Public Worksheet Events

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,832
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Is there any reason why worksheet events, such as:

Code:
Private Sub Worksheet_Activate()

    Msgbox "Hi"

End Sub

are private?

I am thinking of changing it to Public so it can be called elsewhere. Is that a bad idea and might it cause problems?

Alternatively, I can leave it as Private but put the code in a separate module, like this:

Code:
Private Sub Worksheet_Activate()

    Call ModHi.Hi

End Sub

and have a module called ModHi:

Code:
Public Sub Hi()

    Msgbox "Hi"

End Sub

Thanks
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
I am thinking of changing it to Public so it can be called elsewhere. Is that a bad idea and might it cause problems?
Yes, event procedure code runs automatically by certain triggered action, and are not meant to be called.

If you want to call it, put the code in another separate public procedure, and you can call that code within your Worksheet event code, or from wherever else you want.
That way you only have to write the code once, but can call it from multiple places.
 
Upvote 0
Yes, event procedure code runs automatically by certain triggered action, and are not meant to be called.

If you want to call it, put the code in another separate public procedure, and you can call that code within your Worksheet event code, or from wherever else you want.
That way you only have to write the code once, but can call it from multiple places.
Thanks, so as per my alternative solution?
 
Upvote 0
Yep. That is exactly the way you want to do it.
Don't try messing with the Event Procedures.
Thanks for the help. I'll take that approach.

I've been thinking about this for some time but was afraid someone else might ask why is (in my example) the code Sub Hi NOT written in the Sheet's area.

It contradicts the principle "code belonging to a sheet should reside there".
 
Upvote 0
It contradicts the principle "code belonging to a sheet should reside there".
Actually, I wouldn't say that is entirely true (especially when you may have code that interacts between multiple sheets).
The principle that I usually follow is that I typically only put Event Procedure code in the Sheet modules, and put all other code in General Modules (where it is easily accessible from any sheet).
 
Upvote 0
Actually, I wouldn't say that is entirely true (especially when you may have code that interacts between multiple sheets).
The principle that I usually follow is that I typically only put Event Procedure code in the Sheet modules, and put all other code in General Modules (where it is easily accessible from any sheet).
Noted and thanks again.
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,717
Members
448,985
Latest member
chocbudda

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