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.
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.
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.
Can you give some more code, perhaps the whole Sub of Function procedure?
Please leave a message after the beep!
Also, what is the value and its format at?Dividend = ActiveCell.Value
Please leave a message after the beep!
Here you go. I'm attaching the entire thing. Just click on the Refresh button to run the code. Thanks much.
Hi,
It looks like the compiler didn't like a variable in the Dim Statement. You have:
Change this above line toDim Divisors(1 To Dividend) As Variant
and it runs fine for me.Dim Divisors(1 To 500) As Variant
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.
The answer is on your computer: VBEditor F1
You can't dimension an array using a variable. With Redim you can.
you can use:
but you can'tx = 12 ReDim sn(x)
x=12 Dim sq(x)
Last edited by snb; 07-02-2011 at 10:31 AM.
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.
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".
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.
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',Adapt this to your actual code and you're goldenSub 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
Please leave a message after the beep!
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.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks