+ Reply to Thread
Results 1 to 16 of 16

Problem declaring an array

  1. #1
    Registered User
    Join Date
    06-26-2011
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2003
    Posts
    40

    Problem declaring an array

    I'm creating an array to hold integer values. CurrentCell has been set correctly.

    Range(CurrentCell).Select
    ActiveCell.Offset(-2, 0).Select
    Dividend = ActiveCell.Value

    Dim Divisors(1 To Dividend) As Long

    During compile (it's not even running), it tells me that Dividend has to be a "constant" value. It is, so I don't know what's wrong. Tried to use Integer instead of Long as well.

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

    Re: Problem declaring an array

    Hi LydiasDad (When my son turned 12 I became TylersDad, knowing nobody cared about me anymore)

    In the back of my mind is a little bird which says Arrays need to be declared as type variant. I wonder if it could be that easy?

    http://msdn.microsoft.com/en-us/libr...(v=VS.60).aspx
    Last edited by MarvinP; 07-02-2011 at 12:55 AM.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Registered User
    Join Date
    06-26-2011
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2003
    Posts
    40

    Re: Problem declaring an array

    Nope, no luck. Same error. I've researched on the web and there are many examples using Long and String. Don't know why mine doesn't work.

  4. #4
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Problem declaring an array

    Can you give some more code, perhaps the whole Sub of Function procedure?
    If you're happy with someone's help, click that little star at the bottom left of their post to give them Reps.

    ---Keep on Coding in the Free World---

  5. #5
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Problem declaring an array

    Also, what is the value and its format at
    Please Login or Register  to view this content.
    ?

  6. #6
    Registered User
    Join Date
    06-26-2011
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2003
    Posts
    40

    Re: Problem declaring an array

    Here you go. I'm attaching the entire thing. Just click on the Refresh button to run the code. Thanks much.
    Attached Files Attached Files

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

    Re: Problem declaring an array

    Hi,

    It looks like the compiler didn't like a variable in the Dim Statement. You have:
    Please Login or Register  to view this content.
    Change this above line to
    Please Login or Register  to view this content.
    and it runs fine for me.

    I'm reading http://msdn.microsoft.com/en-us/libr...ffice.10).aspx to see if it ever says they must be constants.

  8. #8
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Problem declaring an array

    The answer is on your computer: VBEditor F1

    You can't dimension an array using a variable. With Redim you can.
    you can use:

    Please Login or Register  to view this content.
    but you can't
    Please Login or Register  to view this content.
    Last edited by snb; 07-02-2011 at 10:31 AM.



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

    Re: Problem declaring an array

    Hey snb

    These dim redims are a little discombobulating in my mind.

    Thanks for the edited explanation. This helps me understand them better.

  10. #10
    Registered User
    Join Date
    06-26-2011
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2003
    Posts
    40

    Re: Problem declaring an array

    Still no luck. I used the following:

    Dim Divisors() As Integer
    ReDim Divisors(1 To Dividend)

    I also used ReDim Divisors(Dividend) for that last line.

    Now I get "subscript out of range".

  11. #11
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Problem declaring an array

    The only problem arises in the subscript in combination with 'To'.
    But you can tackle that using 'option base'.

    Please Login or Register  to view this content.
    Last edited by snb; 07-02-2011 at 12:08 PM.

  12. #12
    Registered User
    Join Date
    06-26-2011
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2003
    Posts
    40

    Re: Problem declaring an array

    Hey, thanks for the response, but I don't understand your post. What is snb and msgbox and ubound, and what do they have to do with declaring an array? I just need to declare an array that has up to "Dividend" elements.

  13. #13
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Problem declaring an array

    Quote Originally Posted by MarvinP View Post
    These dim redims are a little discombobulating in my mind.
    I don't know what it is about that line that cracks me up (perhaps the word discombobulating [and yes I've heard the word before]).

  14. #14
    Forum Expert Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,787

    Re: Problem declaring an array

    Quote Originally Posted by LydiasDad View Post
    Hey, thanks for the response, but I don't understand your post. What is snb and msgbox and ubound, and what do they have to do with declaring an array? I just need to declare an array that has up to "Dividend" elements.
    Hi LydiasDad,

    snb is snb, he's trying to help you!
    UBound finds the upper bound or last index of the array. So, the the array is 1 to 3, 3 would be the UBound.
    To declare up to 'Dividend',
    Please Login or Register  to view this content.
    Adapt this to your actual code and you're golden

  15. #15
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Problem declaring an array

    If you are getting a subscript out of range error, then either Dividend is less than 1 (you can't declare an array as 1 to 0 for example) or you referring to an element of the array that doesn't exist in a later piece of code.
    Remember what the dormouse said
    Feed your head

  16. #16
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Problem declaring an array

    Quote Originally Posted by Mordred View Post
    To declare up to 'Dividend',
    Please Login or Register  to view this content.
    This assumes you have an option base 1 statement at the top of the module - the default is base 0, so you should loop from 0, not 1. Better to loop from Lbound to Ubound though.

+ 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