+ Reply to Thread
Results 1 to 9 of 9

How to use the same variable in different procedures

  1. #1
    Forum Contributor
    Join Date
    11-12-2010
    Location
    HK
    MS-Off Ver
    Excel 2010
    Posts
    189

    How to use the same variable in different procedures

    Hi all,

    If I got a variable in a procedure named AAA, how can I use the same variable (ie NewWB) without entering again the right hand side of the equation in the second procedure named BBB in the same module?
    Please Login or Register  to view this content.
    [/CODE]

  2. #2
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: How to use the same variable in different procedures

    declare the variable at the top before your subs, so public NewWB as excel.workbook
    Hope this helps

    Sometimes its best to start at the beginning and learn VBA & Excel.

    Please dont ask me to do your work for you, I learnt from Reading books, Recording, F1 and Google and like having all of this knowledge in my head for the next time i wish to do it, or wish to tweak it.
    Available for remote consultancy work PM me

  3. #3
    Forum Contributor
    Join Date
    11-12-2010
    Location
    HK
    MS-Off Ver
    Excel 2010
    Posts
    189

    Re: How to use the same variable in different procedures

    Quote Originally Posted by nathansav View Post
    declare the variable at the top before your subs, so public NewWB as excel.workbook
    Thanks, but when to enter
    Please Login or Register  to view this content.
    [/CODE]

    I mean in the first procedure or before the first procedure?

  4. #4
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,271

    Re: How to use the same variable in different procedures

    If you want to create global variables that last between workbooks you could read and use this:

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

    If you are going to use it between sheets only then read:

    http://www.worldbestlearningcenter.c...-variables.htm
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  5. #5
    Forum Contributor
    Join Date
    11-12-2010
    Location
    HK
    MS-Off Ver
    Excel 2010
    Posts
    189

    Re: How to use the same variable in different procedures

    Sorry that I am still unclear of this concept.

    Please see below code
    Please Login or Register  to view this content.
    If I would like to use variable NewWB in both sub-procedure, where should I put NewWB = "Sales..........xlsm" in the module? I dont want to put the same statement in every procedure. Any idea is appreciated. Thanks

  6. #6
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,271

    Re: How to use the same variable in different procedures

    Hi,

    See if this works for you...

    In the Workbook module have this code:
    Please Login or Register  to view this content.
    Then in the Module have this sub.
    Please Login or Register  to view this content.
    Of course you need to set RawWB and NewWB as global variables....

    Does this make sense to you??

  7. #7
    Forum Contributor
    Join Date
    11-12-2010
    Location
    HK
    MS-Off Ver
    Excel 2010
    Posts
    189

    Re: How to use the same variable in different procedures

    Quote Originally Posted by MarvinP View Post
    Hi,

    See if this works for you...

    In the Workbook module have this code:
    Please Login or Register  to view this content.
    Then in the Module have this sub.
    Please Login or Register  to view this content.
    Of course you need to set RawWB and NewWB as global variables....

    Does this make sense to you??
    Thanks MarvinP, your idea is good, I am also thinking of using
    Please Login or Register  to view this content.
    before the first procedure.

  8. #8
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,271

    Re: How to use the same variable in different procedures

    Public Const is a great idea also.

    In some other posts, when searching for an answer for you, there was a lot of confusion and concern about using Global variables. One MVP suggested to not use them as they were unreliable. Read
    http://msdn.microsoft.com/en-us/libr...(v=vs.60).aspx
    to see that there is some confusion on using the same variable name and not knowing what was going on. If you use a global constant of NewWB can you change its value in code?? If you create a global variable of NewWB can you change its value in code??

    I believe the confusion happens when you have a Global Variable and then use a DIM statement in a lower module using the same name. Then the confusion starts. Does the code use the most recent Dimed value or the Global value. In my understanding, a procedure is put on the stack and then erased from the program stack at the End Sub statement. End Sub throws away all the variables and code of that Sub. Read the above link to see if following the "Temp" variable makes sense. Also check to see if a Global Constant can change values. I'm not sure it can.

  9. #9
    Forum Contributor
    Join Date
    11-12-2010
    Location
    HK
    MS-Off Ver
    Excel 2010
    Posts
    189

    Re: How to use the same variable in different procedures

    Quote Originally Posted by MarvinP View Post
    Public Const is a great idea also.

    In some other posts, when searching for an answer for you, there was a lot of confusion and concern about using Global variables. One MVP suggested to not use them as they were unreliable. Read
    http://msdn.microsoft.com/en-us/libr...(v=vs.60).aspx
    to see that there is some confusion on using the same variable name and not knowing what was going on. If you use a global constant of NewWB can you change its value in code?? If you create a global variable of NewWB can you change its value in code??

    I believe the confusion happens when you have a Global Variable and then use a DIM statement in a lower module using the same name. Then the confusion starts. Does the code use the most recent Dimed value or the Global value. In my understanding, a procedure is put on the stack and then erased from the program stack at the End Sub statement. End Sub throws away all the variables and code of that Sub. Read the above link to see if following the "Temp" variable makes sense. Also check to see if a Global Constant can change values. I'm not sure it can.
    MarvinP. Much appreciate for your help

+ 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. Event Procedures
    By pflipper in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-30-2009, 01:42 PM
  2. VBA Sub Procedures
    By ladeda063610 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-05-2008, 03:12 AM
  3. Get VBA procedures from DB
    By Hiran de Silva in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-04-2006, 12:50 PM
  4. [SOLVED] organizing procedures
    By Paul in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-28-2005, 07:55 PM
  5. Macro procedures
    By Danita in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-14-2005, 04:06 PM

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