+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: Problem declaring an array

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

    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
    Valued Forum Contributor MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2010
    Posts
    5,300

    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 * below to say thanks.

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

    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 Guru Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,276

    Re: Problem declaring an array

    Can you give some more code, perhaps the whole Sub of Function procedure?
    Please leave a message after the beep!

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

    Re: Problem declaring an array

    Also, what is the value and its format at
    Dividend = ActiveCell.Value
    ?
    Please leave a message after the beep!

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

    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
    Valued Forum Contributor MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2010
    Posts
    5,300

    Re: Problem declaring an array

    Hi,

    It looks like the compiler didn't like a variable in the Dim Statement. You have:
    Dim Divisors(1 To Dividend) As Variant
    Change this above line to
    Dim Divisors(1 To 500) As Variant
    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.
    One test is worth a thousand opinions.
    Click the * below to say thanks.

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

    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:

        x = 12
        ReDim sn(x)
    but you can't
    x=12
        Dim sq(x)
    Last edited by snb; 07-02-2011 at 10:31 AM.



  9. #9
    Valued Forum Contributor MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Seattle, WA
    MS-Off Ver
    Excel 2010
    Posts
    5,300

    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.
    One test is worth a thousand opinions.
    Click the * below to say thanks.

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

    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 Guru snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,151

    Re: Problem declaring an array

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

    Sub snb()
        x = 12
        Dim sr(1)
        ReDim Preserve sr(x + 1, x + 2, x + 3)
        
        MsgBox UBound(sr) & "_" & UBound(sr, 2) & "_" & UBound(sr, 3)
        
        ReDim sn(x)
        MsgBox UBound(sn)
        
        ReDim sq(20, x)
        MsgBox UBound(sq) & "_" & UBound(sq, 2)
    End Sub
    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
    25

    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 Guru Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,276

    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]).
    Please leave a message after the beep!

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

    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',
    Sub LydiasDadsArray()
       Dim LydiasArray() as Long, aCnt as Long  
       ReDim LydiasArray(Dividend)
       For aCnt = 1 to Dividend
          LydiasArray(aCnt) = '''What ever you want it to equal!!'''
       next aCnt
    End Sub
    Adapt this to your actual code and you're golden
    Please leave a message after the beep!

  15. #15
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    Apparently I can't say
    MS-Off Ver
    Apparently I can't say
    Posts
    8,274

    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.

+ 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.2.0