Macro question
Macro question
Thanks Thanks:  0
Likes Likes:  0
Results 1 to 6 of 6

Thread: Macro question

  1. #1
    Board Regular
    Join Date
    Mar 2002
    Location
    Massachusetts, USA
    Posts
    255
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

     
    When writing a macro with the event handler option, is it necessary to add Private?

  2. #2
    MrExcel MVP Damon Ostrander's Avatar
    Join Date
    Feb 2002
    Location
    Denver, Colorado USA
    Posts
    4,240
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    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
    LinkedIn Profile http://www.linkedin.com/pub/damon-ostrander/7/79/a93
    AllExperts Profile http://www.allexperts.com/ep/1059-30...-Ostrander.htm

  3. #3
    Board Regular
    Join Date
    Mar 2002
    Location
    Massachusetts, USA
    Posts
    255
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    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. #4
    MrExcel MVP Damon Ostrander's Avatar
    Join Date
    Feb 2002
    Location
    Denver, Colorado USA
    Posts
    4,240
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

    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
    LinkedIn Profile http://www.linkedin.com/pub/damon-ostrander/7/79/a93
    AllExperts Profile http://www.allexperts.com/ep/1059-30...-Ostrander.htm

  5. #5
    Board Regular
    Join Date
    Mar 2002
    Location
    Massachusetts, USA
    Posts
    255
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default


  6. #6
    Board Regular
    Join Date
    Mar 2002
    Location
    Massachusetts, USA
    Posts
    255
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default

      
    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!!!!!!!

User Tag List

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

 

 
DMCA.com