+ Reply to Thread
Results 1 to 5 of 5

Is there a way to get a declared variable to be remembered in another routine?

  1. #1
    Forum Contributor
    Join Date
    06-06-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    181

    Is there a way to get a declared variable to be remembered in another routine?

    Is there a way to get a declared variable to be remembered in another routine?

    I.e. I have declared the value x below as the ComboBox1.value but when i call 'BuildHCReport' routine the value of x is forgotten i.e. null.

    Any help would be greatly appreciated. Thank you in advance.


    Please Login or Register  to view this content.

  2. #2
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,515

    Re: Is there a way to get a declared variable to be remembered in another routine?

    The concept you are asking about is called a variable's "scope". Here's MSFT's introduction: https://docs.microsoft.com/en-us/off...and-visibility The short answer to your question is decide what scope to give your variable and then structure your project with that scope. Another question is whether to use a single variable with broad scope that many procedures can use or to use multiple procedure level variables and pass the value from procedure to procedure.

    I'm not sure we can provide a solid recommendation based only on the code for one procedure. We need to know more about how this variable fits into your overall project and how you intend to use it. I will say that the general rule of thumb is to declare a variable with the narrowest scope possible for what that variable needs to do. Your description suggests that there is only one other procedure that needs this value. If both procedures are in the same module, then maybe dim x as a module level variable.

    There's also the question of lifetime (another MSFT article: https://docs.microsoft.com/en-us/off...e-of-variables ), which will get into questions of how long each "call" to VBA is and, therefore, how long x will retain its value. Again, that's probably not something we can provide specific advice for without understanding how all these procedures fit into the overall project.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  3. #3
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,973

    Re: Is there a way to get a declared variable to be remembered in another routine?

    In your standard module declare a global variable. Note that it should be out side of any sub. Global variable should not be declared in other modules.
    Ex:
    Please Login or Register  to view this content.
    Then you can set x in your Commandbutton1 sub, and x will retain value when used in another sub.
    "Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something."
    ― Robert A. Heinlein

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,835

    Re: Is there a way to get a declared variable to be remembered in another routine?

    1) You don't need it as you can refer the value of combobox directly form userform like
    Please Login or Register  to view this content.
    2) Or, I would pass the variable to other procedure.

    e.g
    Please Login or Register  to view this content.
    And the other procedure should look like
    Please Login or Register  to view this content.
    Then you can use CBVal in the procedure.

  5. #5
    Forum Contributor
    Join Date
    06-06-2013
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    181

    Re: Is there a way to get a declared variable to be remembered in another routine?

    Thanks Jindon that worked well

+ 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] Excel VBA address a declared Constant by name in a VBA routine
    By Keebellah in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 11-30-2020, 06:42 AM
  2. [SOLVED] Email .Send or .Display as a declared variable
    By Tayque_J_Holmes in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-05-2018, 05:03 AM
  3. Refresh an already declared variable
    By EchoPassenger in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-18-2016, 04:56 AM
  4. Pass a variable from one sub-routine to another sub-routine
    By gowtham_pec in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 05-01-2013, 07:07 PM
  5. [SOLVED] Variable declared as long out as text
    By marreco in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-05-2013, 09:06 AM
  6. [SOLVED] Global variable declared in Userform. Variable value is not sticking
    By timtim89 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 05-25-2012, 08:50 AM
  7. Non-declared variable
    By mqdias in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-20-2007, 10:01 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