+ Reply to Thread
Results 1 to 12 of 12

Option Private Module/code organisation

Hybrid View

  1. #1
    Registered User
    Join Date
    01-03-2012
    Location
    Poland
    MS-Off Ver
    Excel 2007
    Posts
    29

    Option Private Module/code organisation

    Hello, I had no idea how to write a good title for this problem.

    I have a workbook with many different sheets with different macros needed in each sheet.
    Sometimes a sheet is used as a template - VeryHidden and copied when needed.
    Not knowing any better I put my code specific to each sheet in: VBAProject\Microsoft Excel Objects\Sheet...\"View Code" and it worked.

    Now I would like a macro from one sheet to call a procedure in another sheet.
    Which is easy if that procedure is "Public":
    Call Worksheets("Sheet1").testProcedure
    But then that procedure is Public which I do not want (User can try to run it directly from Excel.

    I found that I should use "Option Private Module" which works in "VBAProject\Modules".
    But in Excel objects I get a compile error "Option Private Module not permitted in an object module". And I am stuck.

    I have two problems:
    1. Can it work the way I want it to (a Public procedure in object module not seen by the user)
    2. Is my concept of organizing macros, code is wrong? Are there any tips/tutorials telling how to do it?
    Last edited by Gzdnkh; 06-18-2013 at 04:23 AM.

  2. #2
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Option Private Module/code organisation

    You might need some re-organization of your subs. Generally, I prefer to one module and make the subs private (e.g. Private Sub Test) and call it from within the same module (e.g. Application.Run "Test"). It all depends on what each sub/ function does but I tend to stay away from sheet-level code unless it's specifically needed (e.g. triggered by a worksheet event).

    abousetta
    Please consider:

    Thanking those who helped you. Click the star icon in the lower left part of the contributor's post and add Reputation.
    Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.

  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,290

    Re: Option Private Module/code organisation

    Why not put the common code in a Private module and then call it from both sheets?

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  4. #4
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,788

    Re: Option Private Module/code organisation

    Just tested in this way and its calling even though the procedure is private and picking nicely from any of the module

    Formula: copy to clipboard
    Application.OnTime Now, "YourMacroNameHere"


    If your problem is solved, then please mark the thread as SOLVED>>Above your first post>>Thread Tools>>
    Mark your thread as Solved


    If the suggestion helps you, then Click *below to Add Reputation

  5. #5
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Option Private Module/code organisation

    you may also declare the sheet procedures as Friend
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  6. #6
    Registered User
    Join Date
    01-03-2012
    Location
    Poland
    MS-Off Ver
    Excel 2007
    Posts
    29

    Re: Option Private Module/code organisation

    Thank you for all the help
    @josephP: I have tried "Friend" before and it does not work. The same error as with Private.

    @sixthsense and abousetta: both "Run" and "OnTime" work. A pity I can't pass Variables as with procedures.
    I have a follow up. How to elegantly get strYourMacroNameHere?
    Something like:
    Worksheets(strSheetName).strSubName
    would be nice, but so far I have not found a way to make it work

    Right now I can think only of something like:
    Do Until Worksheets(intCounter).Name = strSheetName Or intCounter Is Worksheets.Count
       intCounter = intCounter + 1
    Loop
    strYourMacroNameHere = "Sheets" & intCounter & "." & "strSubName"

    @TMShucks: That will work in half of the cases, when the procedure is common for all the sheets. The other half of cases would be like:
    I will have several sheets which will need procedures for protecting them, each sheet, different ranges, settings. I imagined creating a procedure called "Protect" in each sheet so i can just use "Call Worksheets(strSheetName).Protect" in main body, when needed. Right now I use "Select Case" and it annoys me

  7. #7
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Option Private Module/code organisation

    Can you put your procedures in a class? That would create sone kind of vba namespace thingy. Your calling code would need to init the class to use it, but its procedures would be out of scope generally. This of course all depends on how your code is structured

  8. #8
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Option Private Module/code organisation

    the attached works for me-only the public sub in sheet1 is visible in the macros dialog but all three can be called (in various ways) from sheet2
    Attached Files Attached Files

  9. #9
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,290

    Re: Option Private Module/code organisation

    Thanks for the rep.

    I generally do not want my code to be visible to the user and/or called manually. If I do, I put it in a standard module that does not have Option Private Module.

    The user cannot see, or call, standard event handlers in sheet or workbook modules.
    The user cannot see subroutines in a Private Module, but they can run them if they know they are there.
    The user cannot see Private subroutines in a standard Module, but they can run them if they know they are there.
    The user cannot see, or call, subroutines that take parameters. So you could add an optional parameter that you do not use.

    Protect your code with a Password to View and it will be difficult for anyone to know what might be lurking behind the scenes.

    Regards, TMS

  10. #10
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Option Private Module/code organisation

    Quote Originally Posted by TMShucks View Post
    The user cannot see, or call, subroutines that take parameters. So you could add an optional parameter that you do not use.
    depends on the parameter-for simple data types if the user knows the routine name he/she can call it from the dialog as with private routines

  11. #11
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,290

    Re: Option Private Module/code organisation

    @JP: thanks for the insight. Never tried that. But, as ever, the user needs to be aware that the subroutine is there and how to call it. I guess most won't.

    Regards, TMS

  12. #12
    Registered User
    Join Date
    01-03-2012
    Location
    Poland
    MS-Off Ver
    Excel 2007
    Posts
    29

    Re: Option Private Module/code organisation

    Thank you all again, I have to digest the information. For now solved

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1