+ Reply to Thread
Results 1 to 8 of 8

Global Variables in Private Module

  1. #1
    Forum Contributor
    Join Date
    10-02-2013
    Location
    Harrisburg, PA
    MS-Off Ver
    Office 365 MSO (16.0.10730.20264) 32-bit
    Posts
    160

    Global Variables in Private Module

    Quick one... To confirm. Is a global variable defined in a private module, before any procedures, available to all modules? In the example below, SQL_SERVER_AVAILABLE & SQL_SERVER_ADODB_CN will be available in all modules
    Thanks.
    Please Login or Register  to view this content.

  2. #2
    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
    44,446

    Re: Global Variables in Private Module

    See: https://learn.microsoft.com/en-us/of...vate-statement
    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


  3. #3
    Forum Contributor
    Join Date
    10-02-2013
    Location
    Harrisburg, PA
    MS-Off Ver
    Office 365 MSO (16.0.10730.20264) 32-bit
    Posts
    160

    Re: Global Variables in Private Module

    Interesting...
    That is not true and incorrect all at the same time. In one excel file, if a module is defined as Private, from any worksheet, when you hit Alt-F8 (ribbon: developer | macros), subs will not be listed for any modules defined as private.

  4. #4
    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
    44,446

    Re: Global Variables in Private Module

    When a module contains Option Private Module, the public parts, for example, variables, objects, and user-defined types declared at the module level, are still available within the project containing the module, but they are not available to other applications or projects.
    It says nothing about subroutines being available, or not.

    Nor does your sample code refer to subroutines.

  5. #5
    Forum Contributor
    Join Date
    10-02-2013
    Location
    Harrisburg, PA
    MS-Off Ver
    Office 365 MSO (16.0.10730.20264) 32-bit
    Posts
    160

    Re: Global Variables in Private Module

    Your link says "Option Private is only useful for host applications that support simultaneous loading of multiple projects". That does not reference subroutines being available or not.
    The original question was about scope of variables in a private module

  6. #6
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    1,980

    Re: Global Variables in Private Module

    The short answer is that your global variables will be available with the Option Private Module. Incidentally, "Public" is more common because it's not limited to being declared in a module.

    The MS explanation isn't very clear IMO and less so because I don't think that cross referencing excel vba projects is very common. The main practical use of Option Private Module appears to be to remove the subs from the macro list.

    Nevertheless, here's how you can demonstrate the effect.
    Save and open two xlsm workbooks Book1 and Book2.
    Add modules to both.
    Change the VBAProjects to VBABook1 and VBABook2 respectively.

    In a Book1 module, declare a public constant Public Const x As Integer = 10
    In a Book2, create a reference to Book1's vba project (see picture)
    In the Book2 module add a sub to show x as a message
    Please Login or Register  to view this content.
    You should find that x will not be available to Book2 if Book1 has Option Private Module.
    Attached Images Attached Images

  7. #7
    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
    44,446

    Re: Global Variables in Private Module

    The main practical use of Option Private Module appears to be to remove the subs from the macro list.
    However, if you know the macro name, you can still type it in and run it, even though it doesn't appear in the macro list.

  8. #8
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    O365 32bit (Windows)
    Posts
    1,980

    Re: Global Variables in Private Module

    shhh

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Using Global variable in module
    By erice in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-23-2018, 04:12 PM
  2. Global constant across whole module
    By malcmail in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-18-2017, 10:29 AM
  3. Global Variable in a module
    By aman1234 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-21-2015, 10:08 AM
  4. Global Variables being used in a spreadsheet module
    By ChemistB in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 05-04-2011, 12:37 PM
  5. Accessing Global Variables defined in ThisWorkbook Module
    By smz in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-02-2010, 08:26 PM
  6. Private modules and global variables
    By davidm in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-25-2005, 03:15 PM
  7. [SOLVED] Global (Module) Variable Problem
    By Allen Geddes in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-21-2005, 11:40 AM

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