ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   How to Access Array of Arrays? (https://www.excelbanter.com/excel-programming/323958-how-access-array-arrays.html)

billbell52

How to Access Array of Arrays?
 
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


Tom Ogilvy

How to Access Array of Arrays?
 
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.

--
Regards,
Tom Ogilvy


"billbell52" wrote in message
ups.com...
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





All times are GMT +1. The time now is 07:54 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com