+ Reply to Thread
Results 1 to 6 of 6

static global variable?

  1. #1
    Registered User
    Join Date
    05-24-2004
    Posts
    52

    static global variable?

    Is there any way to make a variable accessible to all the procedures in a project while still making the value of that variable static once it gets assigned/calculated??

    I have at least one variable that I'd like to use & refer to (not always in the same order) in at least half a dozen macros, but I want to make sure that the variable's value stays the same. I suppose I could theoretically reset & recalculate the variable at the start of each macro, but I'd definitely prefer not to.

    So is there anything I can do to get a static global variable??

  2. #2
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    On a normal module sheet declare your variable as Public. These are available to all macros in the workbook.

    Please Login or Register  to view this content.
    If you know what the variable is to be set to now and it will always ne the same then you can use constants

    Please Login or Register  to view this content.
    Please Read Forum Rules Before Posting
    Wrap VBA code by selecting the code and clicking the # icon or Read This
    How To Cross Post politely

    Top Excel links for beginners to Experts

    If you are pleased with a member's answer then use the Scales icon to rate it
    If my reply has assisted or failed to assist you I welcome your Feedback.

  3. #3
    Registered User
    Join Date
    05-24-2004
    Posts
    52
    Thanks mudraker but unfortunately I don't think declaring it as a Public Const will work for me since the item that I need to be global & static is actually a Code Module (called 'CM'), not an integer or string etc. According to the VB Help File....
    Constants can be declared as one of the following data types: Boolean, Byte, Integer, Long, Currency, Single, Double, Date, String, or Variant.
    And if I just declare CM to be Public at the start of the whole module (or of each procedure it's used in), then won't its 'value' or 'definition' reset at the start of each procedure that's called? Or does declaring it as Public there mean that its definition/value is preserved across all macros & functions?? (I hope so!!)

    Since nearly all of my macros (let's refer to them as 'primary codes') call 1 of 3 other CM-using procedures ('secondary codes') before they refer to CM themselves, what if I declared "Static CM as CodeModule" in each of those 3 secondary codes and then "Dim CM as CodeModule" in all of the primaryones? Then VB would be happy because CM would be declared at that start of all the primary codes, but because CM wouldn't actually be defined until a primary code called a secondary code, its 'value' would actually be Static because that's what the secondary code declared it would be. (follow all that??)

    Would that work? Would declaring it as Public at the top of the module be enough? Or am I out of luck & just have to redefine CM at the start of all primary AND secondary codes that use it?

    Thanks for your help so far!!

  4. #4
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    The public variable is preserved across all macros & functions

    Try this test - Copy all the following code to a new module sheet

    Please Login or Register  to view this content.
    Do not declare the same variable name as public & also within a macro If you do then the macro will use the one declared within it & not the public declared variable

    Try this test - code exactly the same as above except for macro4

    Please Login or Register  to view this content.

    I hope this helps you understand using Public variables better.

    Also only use Public declared variables as a last resort not as a norm

  5. #5
    Registered User
    Join Date
    05-24-2004
    Posts
    52
    excellent. thanks so much. that's really helped a lot!

  6. #6
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    Here is a little more reading for you that help to explain more on declaring variables

    The 2nd one is proberly a little too heavy for your needs at the moment

    http://www.cpearson.com/excel/variables.htm
    http://www.cpearson.com/excel/TrulyGlobalVariables.htm

+ 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