How to Access Array of Arrays?

B

billbell52

I have a variant array (vVal) that contains an arrays of type single.
In the for loop I (1) redim to 0, (2) extend array by 1, (3)
set value in array. The code I have there is wrong but sort of
shows what I am trying to do.


Dim v1() As Single
Dim v2() As Single
Dim v3() As Single
Dim vKey() As Variant
Dim vVal() As Variant
Dim i As Integer, k As Integer, j As Integer
Dim m As Integer

vKey = Array("VOLTAGES", "TEMPERATURES", "ROWS", "COLS",
"SENSE_AMPS")
vVal = Array(v1(), v2(), v1(), v2(), v3())
' Loop to test access
For i = 0 To UBound(vVal)
ReDim vVal(i)(0) ' Redim to 0
ReDim Preserve vVal(i)(UBound(vVal(i) + 1)) ' Add 1 item to array
vVal(i)(UBound(vVal(i)) = 125 ' Set value
Next i
 
T

Tom Ogilvy

Sub AA()
Dim v1() As Single
Dim v2() As Single
Dim v3() As Single
Dim vKey() As Variant
Dim vVal() As Variant
Dim v As Variant
Dim i As Integer, k As Integer, j As Integer
Dim m As Integer, ub As Variant

vKey = Array("VOLTAGES", "TEMPERATURES", "ROWS", "COLS", "SENSE_AMPS")
vVal = Array(v1, v2, v3, v4, v5)
' Loop to test access
For j = 1 To 3
For i = LBound(vVal) To UBound(vVal)
v = vVal(i)
ub = Empty
On Error Resume Next
ub = UBound(v)
On Error GoTo 0
If IsEmpty(ub) Then
ReDim v(0 To 0)
Else
ReDim Preserve v(ub + 1) ' Add 1 item to array
End If
v(UBound(v)) = CSng(Format((Rnd() * 1000 + 1), "0.00"))
vVal(i) = v
Next i
Next j
s = ""
For i = LBound(vVal) To UBound(vVal)
s = s & i & ": "
For j = LBound(vVal(i)) To UBound(vVal(i))
s = s & vVal(i)(j) & ", "
Next
s = s & vbNewLine
Next
MsgBox s
End Sub

If you are trying to have V1 appear twice, v2 appear twice and v3 appear
once as you show,

vVal = Array(v1(), v2(), v1(), v2(), v3())

then that won't work. vVal holds a copy of the arrays assigned.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top