+ Reply to Thread
Results 1 to 11 of 11

Global Array not working

  1. #1
    Registered User
    Join Date
    11-20-2010
    Location
    Delaware, USA
    MS-Off Ver
    Excel 2003
    Posts
    73

    Global Array not working

    Good Morning,

    I've been putting together a UserForm that utilizes an array in 2 different subs. I want to build the array in 1 sub and then reference it in another. I'm getting:


    Compile Error:
    Sub or Function not defined


    I believe I declared the array corrected as public in the "Workbook_Open" section.

    Please Login or Register  to view this content.
    Then in my userform I get:

    Please Login or Register  to view this content.
    Any advice? I'm at a dead end!

    John
    Last edited by Pergo; 08-30-2012 at 11:27 AM.

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

    Re: Global Array not working

    You can't declare a public variable in any procedure, and you can't declare a public variable in a class module (sheet module, ThisWorkbook, form, user-defined class).

    Public variables must be declared at the top of a standard module, above any procedures, right below Option Explicit.
    Entia non sunt multiplicanda sine necessitate

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

    Re: Global Array not working

    @shg,
    you mean a public array variable. other types (apart from fixed length strings and udts) you can declare as public in a class-they just become public members of the class.
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  4. #4
    Registered User
    Join Date
    11-20-2010
    Location
    Delaware, USA
    MS-Off Ver
    Excel 2003
    Posts
    73

    Re: Global Array not working

    so does that mean it won't work because it's an array?

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

    Re: Global Array not working

    it means you need to declare it in a normal module, as shg said-not in ThisWorkbook or a worksheet/class module.

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

    Re: Global Array not working

    you mean a public array variable. other types (apart from fixed length strings and udts) you can declare as public in a class-they just become public members of the class.
    No, I think I mean any public variable -- scalar, object, or array. A variable declared as public in a class module is scoped local to an instance of the class. In the case of worksheet and workbook modules, there can only be a single instance, so they can seem as public variables (prefixed with the class), but I think it's a bit of perversion.

  7. #7
    Registered User
    Join Date
    11-20-2010
    Location
    Delaware, USA
    MS-Off Ver
    Excel 2003
    Posts
    73

    Re: Global Array not working

    Okay, so I made a new normal Module

    Please Login or Register  to view this content.
    I'm still getting the same error. Sorry, I"m fairly new to this stuff.

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

    Re: Global Array not working

    Just to repeat,

    You can't declare a public variable in any procedure, and you can't declare a public variable in a class module (sheet module, ThisWorkbook, form, user-defined class).

    Public variables must be declared at the top of a standard module, above any procedures, right below Option Explicit.

    Please Login or Register  to view this content.
    Last edited by shg; 08-30-2012 at 10:00 AM.

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

    Re: Global Array not working

    I take your point, though I would suggest that in any practical way it is a public variable (not least because it is a variable declared as Public). the issue is of course moot here anyway since an array cannot be a public member of a class.

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

    Re: Global Array not working

    Fair enough.

  11. #11
    Registered User
    Join Date
    11-20-2010
    Location
    Delaware, USA
    MS-Off Ver
    Excel 2003
    Posts
    73

    Re: Global Array not working

    Thanks so much!!!

    Okay, so that solved the problem of it breaking-- it no longer breaks, but it still doesn't retain the information like I need it to.

    I made a mock-example of what I'm trying to do, it's attached EXAMPLE.xlsm and also here is the code:

    Please Login or Register  to view this content.
    So I'm trying to have an e-mail go out when you hit Submit that lists everything that was input. But it only seems to take what was last input.
    Last edited by Pergo; 08-30-2012 at 10:30 AM.

+ 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