View Full Version : Initialize arrays in VBA
General Kenobi (ret.)
02-28-2002, 03:07 PM
I want to fill a 24x3 array with constant values. In the good old days of BASIC, I could have used READ and DATA statements with FOR/NEXT loops to do this, but VBA doesn't seem to have READ and DATA statements anymore. I guess I could encode the data in strings and peel off pieces, but that's a dreadful hack. Is there a better way?
NoName
02-28-2002, 03:11 PM
Assuming you're using Excel, you can put the values on a worksheet and then read them into the array. Otherwise I think you will have to initialize laboriously (x(1,1)=2: x(1,2)=6: etc.), or using text as you suggest or by reading in a text file.
General Kenobi (ret.)
02-28-2002, 03:14 PM
It's in Access, and reading the values out of a table occurred to me, but that's a pain too.
urysohn
02-28-2002, 03:36 PM
Not the ideal solution but try creating three arrays with 24 constants each (typed in), then use code to make one 24x3 array?
Simpler example with a 5x2 array...
<-----Begin Code------->
Dim BigArray(1 To 5, 1 To 2)
Array1 = Array(1, 2, 3, 4, 5)
Array2 = Array(9, 8, 7, 6, 5)
For i = 1 To 5
BigArray(i, 1) = Array1(i - 1)
BigArray(i, 2) = Array2(i - 1)
Next i
<-------End Code--------->
General Kenobi (ret.)
02-28-2002, 03:44 PM
I'll give that one a try. It sure beats the string method.
The upside is that I only have to do this to initialize the array, so processing efficiency isn't a huge consideration. If I had to do it repeatedly in a loop, I'd be really ticked.
NoName
02-28-2002, 04:18 PM
Come to think of it...
big=Array(Array(1, 2, 3, 4, 5), _
Array(6, 7, 8, 9, 10))
The elements can then be accessed as
big(0)(0)
etc.
vBulletin® v3.7.6, Copyright ©2000-2013, Jelsoft Enterprises Ltd.