Macro question

nancyo

Active Member
Joined
Mar 25, 2002
Messages
255
When writing a macro with the event handler option, is it necessary to add Private?
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Hi Nancyo,

I assume you are simply referring to any routine in an event module, whether it be a worksheet, workbook, userform, etc. All routines in any of these modules are automatically private, and you can't make them otherwise whether you use the Private keyword or not. The Public keyword is even overridden. So the Private keyword is not necessary. But you might want to use it anyway as a reminder that the routine is in actuality private.
 
Upvote 0
I am brand new to macros, so please excuse my ignorance. Under the help menu for a Sub Statement: Private = indicates that the Sub procedure is accessible ONLY to other procedures in the module where it is declared, and is optional. Public = is accessible to all other procedures in all modules. I guess I don't understand the difference...
 
Upvote 0
Hi again Nancy,

There are several differences. If a procedure in a macro module (not an event code module) is declared Private, it cannot be called from any other module. In addition, another module could have a procedure that has the same name, and the two would not conflict: each module would have its own copy of the procedure, and the procedures would not necessarily do the same thing. In addition, a Private procedure will not show up in the list of macros you get when you go to Tools -> Macro -> Macros, so it cannot be run from the Excel interface.

If you declare a procedure as Public in a macro module, it shows up in the Excel interface. In addition, it can be called from within a procedure in any other module in Excel (even other workbooks), INCLUDING the event code modules.

Procedures in macro modules are Public by default (i.e., if declared without the Private or Public keyword), while procedures in event modules are private no matter what.

The same principle applies to declaring variables. A Public variable can be accessed from any code anywhere in Excel, while a Private variable can only be accessed from within the code module it is declared in. Variables are private by default (i.e., if declared without a Public or Private keyword)
 
Upvote 0
OK - I think I get it! I am setting up large automated spreadsheets (some with up to 100 different sheets!), and don't want anyone to accidentally change anything. THANKS!!!!!!!
 
Upvote 0

Forum statistics

Threads
1,213,484
Messages
6,113,924
Members
448,533
Latest member
thietbibeboiwasaco

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