+ Reply to Thread
Results 1 to 5 of 5

passing a variable to an array declaration

  1. #1
    Registered User
    Join Date
    09-01-2011
    Location
    Austria
    MS-Off Ver
    Excel 2007
    Posts
    78

    passing a variable to an array declaration

    I'm trying to declare an array with a size which varies depending on the number of columns in use (I'm building a check into the workbook open).

    When I use :
    Static nColorIndices(1 To cnNUMCOLS) it returns "constant expression required" whenever cnNUMCOLS is a variable. This variable will only change when you open the workbook though.

    Any ideas of a way around this?

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: passing a variable to an array declaration

    Hello labogola,

    Here is an example of passing in the variable to set the array's size.
    Please Login or Register  to view this content.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    09-01-2011
    Location
    Austria
    MS-Off Ver
    Excel 2007
    Posts
    78

    Re: passing a variable to an array declaration

    Hi Leith,
    I'm still having some issues with it.

    There is a public variable declared in a module.

    Please Login or Register  to view this content.
    Upon startup these variables are both defined by another script which counts rows and columns. I have tested it, and the output is fine.

    However, when I run this script below it starts spitting out subscript out of range errors and the public variables are no longer defined as they were.


    Please Login or Register  to view this content.
    I don't get why that changes the public variables as I'm not asking it to change anything.
    Last edited by labogola; 01-08-2012 at 10:42 PM.

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: passing a variable to an array declaration

    Hello labogola,

    The Public variables should not change because of a run-time error. I think it best if you post your workbook for review.

  5. #5
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: passing a variable to an array declaration

    If you have unhandled errors, it tends to cause state loss and your variables will get reset to their default values.
    Good luck.

+ 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