+ Reply to Thread
Results 1 to 3 of 3

Question re Workbook level declarations

  1. #1
    VanS
    Guest

    Question re Workbook level declarations

    Hello,
    Trying to meet deadline but couldn't find anything in MS documentation re
    making variable declarations at the Workbook level under ThisWorkbook. If I
    make such a variable declaration there with Option Explicit and Public, will
    it be available to all Worksheets and modules code? I created a boolean such
    var and one worksheet code module recognized it but another cried "Variable
    Not Defined".
    I was trying to avoid putting it into a code module if possible.
    Thanks, God bless
    Van

  2. #2
    GS
    Guest

    RE: Question re Workbook level declarations

    Hi VanS,

    Worksheet and ThisWorkbook module are private, thus the message regarding
    the variable in the other code module.

    To make a variable global, place it in a standard module, outside any
    procedures. This is usually done in a MGlobals module (if you use one), at
    the top in the Declarations section. Declaring it Public makes it accessible
    to all components of your project while the project is open.

    HTH
    Regards,
    GS

    "VanS" wrote:

    > Hello,
    > Trying to meet deadline but couldn't find anything in MS documentation re
    > making variable declarations at the Workbook level under ThisWorkbook. If I
    > make such a variable declaration there with Option Explicit and Public, will
    > it be available to all Worksheets and modules code? I created a boolean such
    > var and one worksheet code module recognized it but another cried "Variable
    > Not Defined".
    > I was trying to avoid putting it into a code module if possible.
    > Thanks, God bless
    > Van


  3. #3
    Valued Forum Contributor tony h's Avatar
    Join Date
    03-14-2005
    Location
    England: London and Lincolnshire
    Posts
    1,187
    you should be ok to create a public variable in any module. If you have to use them place them straight after the option explicit eg:

    Option Explicit

    Public blnVar As Boolean

    Don't place them inside a subroutine.

    Personally I avoid them like the plague. They make debuging a piece of code fraught with difficulties. If I do I create a code module called Globals so that they are clearly obvious to everyone. But my preferred method is to create a class module to hold global values as properties.

    regards

+ 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