+ Reply to Thread
Results 1 to 9 of 9

Declaring global variables

  1. #1
    Registered User
    Join Date
    03-09-2012
    Location
    Cape Town, South Africa
    MS-Off Ver
    Excel 2010
    Posts
    61

    Declaring global variables

    Hi,

    I'm trying to access a variable in sub 2 which was declared in sub 1, both within the same module. How do I do this i.e. I want to declare a variable in sub 1 such that its accessable in sub 1 and sub 2. I've tried declaring the variable as global above both subs (in the declarations section), but it doesn't seem to be working

    Thank you!

  2. #2
    Forum Contributor
    Join Date
    09-08-2011
    Location
    Leicester, England
    MS-Off Ver
    Excel 2007
    Posts
    157

    Re: Declaring global variables

    example, enter this at the top of a general module (eg Module1).
    Please Login or Register  to view this content.
    Then refer to it in your worksheet event code as you would any other variable. For example.

    Please Login or Register  to view this content.
    hope this helps

  3. #3
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,605

    Re: Declaring global variables

    In (General)declarations declare variable like this and try.

    Please Login or Register  to view this content.

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,437

    Re: Declaring global variables

    You can't if the variable was declared in sub1. You would need to pass it into sub2 as an argument.
    Or move the declaration outside of sub1 and have it available to all routines in the module.

    If you declare the variable in sub1 AND outside of all routines the variables used in sub1 and sub2 will be different variables with different scope but with the same name.
    Cheers
    Andy
    www.andypope.info

  5. #5
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Declaring global variables

    you can declare a module level variable that is used by both bits of code (but do NOT declare the same variable again in either code) or you can pass the variable from one routine to the other which is usually better

  6. #6
    Registered User
    Join Date
    03-09-2012
    Location
    Cape Town, South Africa
    MS-Off Ver
    Excel 2010
    Posts
    61

    Re: Declaring global variables

    Thanks for the replies.

    I'm not sure what I'm doing wrong, but I tried those suggestions and I'm not getting it to work. Here is a sample copy of my code:
    Please Login or Register  to view this content.
    And its throwing an error when I run sub temp, saying that object is required at the "consolidatedFile.activate" line

    Any help would be awesome! Thanks!
    Last edited by arlu1201; 04-19-2012 at 05:59 AM. Reason: Use code tags in future as per forum rules.

  7. #7
    Registered User
    Join Date
    03-09-2012
    Location
    Cape Town, South Africa
    MS-Off Ver
    Excel 2010
    Posts
    61

    Re: Declaring global variables

    Ok, no problem, I sorted it out. Thanks so much to everyone for their help, much appreciated!

  8. #8
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Declaring global variables

    Quote Originally Posted by MikeFranz123 View Post
    its throwing an error when I run sub temp, saying that object is required at the "consolidatedFile.activate" line
    that line ain't in the code you posted. if it's in your real code then the problem is that you're trying to use a string as a workbook.

  9. #9
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,437

    Re: Declaring global variables

    Please Login or Register  to view this content.
    I assume you got it to work by running the other sub first.

    Also I assume your example is contrived as I don't see the point of activating the activeworkbook.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

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