+ Reply to Thread
Results 1 to 13 of 13

Empty Global Array

  1. #1
    Registered User
    Join Date
    12-13-2012
    Location
    Raleigh, NC
    MS-Off Ver
    Excel 2010
    Posts
    20

    Empty Global Array

    I'm not really sure what the best way to declare global or public variables is, but I've declared a public array in a module as shown in the picture below. However, when my sub is executed, it says my public array "Pattern" is empty. What am I doing wrong?

    excel.png

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Empty Global Array

    Since you've got the declaration within the subroutine, it expires at end sub.

    Put the declaration up under the Option Explicit and run the sub before you try to use it in another routine.
    Last edited by xladept; 08-14-2017 at 02:20 PM. Reason: Corrected logical mistake/ shg & Jeff
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  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: Empty Global Array

    You can't declare a variable as public in a procedure -- your code doesn't compile. It needs to go at the top of the module:

    Please Login or Register  to view this content.
    Also, the default lower bound of an array is 0, not 1. You should declare bounds explicitly.
    Entia non sunt multiplicanda sine necessitate

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Empty Global Array

    You are confusing the concept of global variables vs. public variables. Before I start explaining, they should both be used sparingly. You also have a problem with variable declarations.

    I strongly recommend to everyone that they declare variables. Doing so prevents a lot of bug and runtime errors. In your case, you are not using Option Explicit so if you use a variable without declaring it, VBA will not tell you it's an error--it just assumes you meant to declare it, and it declares it for you with a type of Variant. I'll come back to that in a minute.

    A global variable is one that has scope and extant through an entire module. That is, it can be referenced by code anywhere in that module and its value is preserved throughout execution. (There is an exception to this called hiding that I'm not going to get into.) A global variable must be declared at the top of a module, before the first Sub. There is no keyword for global; if it declared at the top of the module, it is global.

    A public variable is a global variable that has scope and extant through all modules. A Public variable is also global, and so must be declared at the top of a module, before the first Sub.

    Your variable Pattern has been declared as Public, but you declared it inside a Sub. If you move your declaration to the top of the module, it will fix this.

    Also, the place you use Pattern does not use Option Explicit. VBA does not see a declaration anywhere for Pattern that is in scope for where you are using it, so it simply creates a new variable for you and assumes it to be type Variant. This variable Pattern is a different variable than the Pattern you declared in Sub DeclareGlobalVariables. Because this new variable that has been so conveniently created has never received a value, you get a message that it's empty.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  5. #5
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Empty Global Array

    Quote Originally Posted by xladept View Post
    [...]it should be a Variant rather than a String.
    It appears that String is an appropriate type to use in this case. Every element of the array is a String.

  6. #6
    Registered User
    Join Date
    12-13-2012
    Location
    Raleigh, NC
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Empty Global Array

    Quote Originally Posted by shg View Post
    You can't declare a variable as public in a procedure -- your code doesn't compile. It needs to go at the top of the module:

    Please Login or Register  to view this content.
    Also, the default lower bound of an array is 0, not 1. You should declare bounds explicitly.
    I've reserved 0 as a non-match for a pattern in a sub, so that's the reason why it's not declared. Are you saying that I should avoid declaring bounds like this:

    Please Login or Register  to view this content.
    and instead, do this:

    For i = 2 To 12

  7. #7
    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: Empty Global Array

    I've reserved 0 as a non-match for a pattern in a sub, so that's the reason why it's not declared.
    That's not a good reason.

    Are you saying that I should avoid declaring bounds like this:
    No, that part's fine (but that's not a declaration, those are iteration limits).

  8. #8
    Registered User
    Join Date
    12-13-2012
    Location
    Raleigh, NC
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Empty Global Array

    Quote Originally Posted by 6StringJazzer View Post
    You are confusing the concept of global variables vs. public variables. Before I start explaining, they should both be used sparingly. You also have a problem with variable declarations.

    I strongly recommend to everyone that they declare variables. Doing so prevents a lot of bug and runtime errors. In your case, you are not using Option Explicit so if you use a variable without declaring it, VBA will not tell you it's an error--it just assumes you meant to declare it, and it declares it for you with a type of Variant. I'll come back to that in a minute.

    A global variable is one that has scope and extant through an entire module. That is, it can be referenced by code anywhere in that module and its value is preserved throughout execution. (There is an exception to this called hiding that I'm not going to get into.) A global variable must be declared at the top of a module, before the first Sub. There is no keyword for global; if it declared at the top of the module, it is global.

    A public variable is a global variable that has scope and extant through all modules. A Public variable is also global, and so must be declared at the top of a module, before the first Sub.

    Your variable Pattern has been declared as Public, but you declared it inside a Sub. If you move your declaration to the top of the module, it will fix this.

    Also, the place you use Pattern does not use Option Explicit. VBA does not see a declaration anywhere for Pattern that is in scope for where you are using it, so it simply creates a new variable for you and assumes it to be type Variant. This variable Pattern is a different variable than the Pattern you declared in Sub DeclareGlobalVariables. Because this new variable that has been so conveniently created has never received a value, you get a message that it's empty.
    Thanks for the clear explanation. One other thing I'm curious about is that my primary sub is under a worksheet... should this sub be placed within a module underneath the global variable declaration instead of it being a worksheet module?

  9. #9
    Registered User
    Join Date
    12-13-2012
    Location
    Raleigh, NC
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Empty Global Array

    Quote Originally Posted by shg View Post
    You can't declare a variable as public in a procedure -- your code doesn't compile. It needs to go at the top of the module:

    Please Login or Register  to view this content.
    Also, the default lower bound of an array is 0, not 1. You should declare bounds explicitly.
    Ok, I see. I missed your part about declaring the size of the array explicitly.

  10. #10
    Registered User
    Join Date
    12-13-2012
    Location
    Raleigh, NC
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Empty Global Array

    It looks like what I'm trying to do won't work with VBA anyway. I wanted to declare and initialize the array elements to be used in other subs or functions, but I get a compile error for invalid outside procedure.

  11. #11
    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: Empty Global Array

    That's why you put the initialization inside a procedure. Run Init first.

    Please Login or Register  to view this content.

  12. #12
    Registered User
    Join Date
    12-13-2012
    Location
    Raleigh, NC
    MS-Off Ver
    Excel 2010
    Posts
    20

    Re: Empty Global Array

    Quote Originally Posted by shg View Post
    That's why you put the initialization inside a procedure. Run Init first.

    Please Login or Register  to view this content.
    Ok, cool. So, I can put the initialization inside a separate sub and then call that sub within another sub.

  13. #13
    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: Empty Global Array

    Yup .

+ 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. Global Array
    By peternorth in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-16-2013, 10:12 AM
  2. global 2 column array
    By oscar07202 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-19-2012, 01:58 PM
  3. [SOLVED] Global Array not working
    By Pergo in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 08-30-2012, 10:25 AM
  4. How to use a global array correctly.
    By Brad4444 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 04-08-2011, 03:01 PM
  5. Global Array - How To Size
    By DCSwearingen in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-10-2007, 12:13 PM
  6. [SOLVED] public or global array
    By shishi in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-06-2006, 05:35 PM
  7. Global array declare
    By Souris in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-20-2005, 07:05 AM

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