![]() |
![]() |
|
|||||||
| Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Board Regular
Join Date: Mar 2002
Location: Massachusetts, USA
Posts: 255
|
When writing a macro with the event handler option, is it necessary to add Private?
|
|
|
|
|
|
#2 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Denver, Colorado USA
Posts: 4,014
|
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.
__________________
Keep Excelling. Damon VBAexpert Excel Consulting (My other life: http://damonostrander.com ) |
|
|
|
|
|
#3 |
|
Board Regular
Join Date: Mar 2002
Location: Massachusetts, USA
Posts: 255
|
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...
|
|
|
|
|
|
#4 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Denver, Colorado USA
Posts: 4,014
|
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)
__________________
Keep Excelling. Damon VBAexpert Excel Consulting (My other life: http://damonostrander.com ) |
|
|
|
|
|
#5 |
|
Board Regular
Join Date: Mar 2002
Location: Massachusetts, USA
Posts: 255
|
|
|
|
|
|
|
#6 |
|
Board Regular
Join Date: Mar 2002
Location: Massachusetts, USA
Posts: 255
|
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!!!!!!!
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|