+ Reply to Thread
Results 1 to 4 of 4

How long do vars keep their values in VBA?

  1. #1
    Forum Contributor
    Join Date
    09-28-2007
    Location
    New York, NY
    MS-Off Ver
    2007
    Posts
    120

    How long do vars keep their values in VBA?

    Hello all,

    I noticed that sometimes if I stop a macro midway (completely stop, not just pause), and then re-run it from the beginning, my variables will still hold the value they were last set to right before i stopped the macro. But sometimes, this doesn't happen - the variables reset.

    I'm wondering what defines how long the variable's value are held for? I'm considering writing a macro that can hold variables so that even after the macro is finished, the user can re-run and the variables will still be available. This is useful mostly for text boxes in userforms that will probably have the same values the next time the user runs, but the user can still change them if he wants.


    Thanks
    Joe
    Last edited by MCCCLXXXV; 02-10-2009 at 11:00 AM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: How long do vars keep their values in VBA?

    Hello Joe,

    The lifetime of a variable is determined by where it is declared. Variables declared in the body of a Sub or Function hold their value only as long as the Sub or Function is active. However, declaring a variable as Static within a Sub or Function will retain its value between calls. Variables declared as Public at the top of a standard Module hold their values for as long as the project is active.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: How long do vars keep their values in VBA?

    Variable retain their values until they go out of scope or are explicitly changed (e.g., Erase for an array).

    Procedure-level variables (those declared in a Sub or Function) go out of scope when the procedure exits, unless they are declared as Static.

    Module-level and global (Public) variables go out of scope when anybody executes an End statement or you hit the Reset button.
    Entia non sunt multiplicanda sine necessitate

  4. #4
    Forum Contributor
    Join Date
    09-28-2007
    Location
    New York, NY
    MS-Off Ver
    2007
    Posts
    120

    Re: How long do vars keep their values in VBA?

    thanks...working so far.

+ 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