PDA

View Full Version : Solved: Redim Preserve

mferrisi
06-12-2007, 07:38 AM
I am attemping to create a multi array of unknown size, which holds an id and a date within a loop. The following cose gives me a 'subscript outn of range' error. Any suggestions?

thank you

Dim Info() As Variant
Dim ijk As Integer
Dim x As integer

For x = 1 to 1000
If (some event) Then
ReDim Preserve Info(1 To ijk, 1 To 2)
Info(ijk, 1) = Id
Info(ijk, 2) = TempW(x, 5)
ijk = ijk + 1
Else End if
Next x

mvidas
06-12-2007, 07:44 AM
mferrisi,

"ijk" is never being given an initial value, so at first run you are dimming info as "(1 to 0, 1 to 2)"

That being said, you'll have to switch the "1 to 2" and "1 to ijk", as you can only increase the highest dimension of the array:Dim Info() As Variant
Dim ijk As Integer
Dim x As Integer

ijk = 1
For x = 1 To 1000
If (some event) Then
ReDim Preserve Info(1 To 2, 1 To ijk)
Info(1, ijk) = ID
Info(2, ijk) = TempW(x, 5)
ijk = ijk + 1
End If
Next x Lastly, dim 'x' and 'ijk' as Long, as you're using VBA for this and all Integers get converted to Long Integers at runtime anyways in VBA for office 2000+