ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Changing values of an array of arrays (https://www.excelbanter.com/excel-programming/391105-changing-values-array-arrays.html)

Joe Dunfee

Changing values of an array of arrays
 
I am attempting to use an array of arrays (ragged arrays), but am having
problems.

It seems the master-array elements must set equal to the sub-arrays only
AFTER the values are entered for the sub-arrays. Then it seems the values of
the elements are set and cannot be changed. Here is my sample
code with comments (my next question follows);

'==================
Sub ABC()
Dim vArr1(1 To 10) As Variant
Dim vArr2(1 To 5) As Variant
Dim vMainArr(1 To 2) As Variant
Dim lCounter As Long
Dim s As String, i As Long, j As Long

' Populate the sub arrays:
For lCounter = 1 To 10
vArr1(lCounter) = lCounter
If lCounter < 6 Then _
vArr2(lCounter) = lCounter * 2
Next lCounter

' Assign the sub arrays to the main array:
' Note that if this isn't done after are populated with values, the array
will be empty.
vMainArr(1) = vArr1
vMainArr(2) = vArr2

'change one of the elements as a test
vArr2(2) = 100 ' note that this change WON'T show in the vMainArr array

' Show the results
s = ""
For i = 1 To 2
For j = LBound(vMainArr(i), 1) To UBound(vMainArr(i), 1)
s = s & vMainArr(i)(j) & ","
Next j
s = s & vbNewLine
Next i
MsgBox s

End Sub

'==================

If my issues above are true, then I need to figure out a way around these
limitations. Note that my actual program would have perhaps 200 elements in
the main array, and 20 sub elements.of mixed type (strings and numbers), then
about 5 of the sub-sub elements would be 1-dimensional arrays. Just using 200
separate arrays would not be desirable.

Perhaps I must repeat the lines where sub arrays are assigned to the main
array EVERY time I change a value.

Any other suggestions?

Joe Dunfee

Tom Ogilvy

Changing values of an array of arrays
 
You are correct that vArr1 and vArr2 have no relation to the array of arrays.
The MainArr(2) does not hold a pointer to vArr2. So if you want to change
the value in the MainArr(2), second position, then do it directly

'==================
Sub ABC()
Dim vArr1(1 To 10) As Variant
Dim vArr2(1 To 5) As Variant
Dim vMainArr(1 To 2) As Variant
Dim lCounter As Long
Dim s As String, i As Long, j As Long

' Populate the sub arrays:
For lCounter = 1 To 10
vArr1(lCounter) = lCounter
If lCounter < 6 Then _
vArr2(lCounter) = lCounter * 2
Next lCounter

vMainArr(1) = vArr1
vMainArr(2) = vArr2

'change one of the elements as a test
vMainArr(2)(2) = 100

' Show the results
s = ""
For i = 1 To 2
For j = LBound(vMainArr(i), 1) To UBound(vMainArr(i), 1)
s = s & vMainArr(i)(j) & ","
Next j
s = s & vbNewLine
Next i
MsgBox s

End Sub

worked for me.

--
Regards,
Tom Ogilvy


"Joe Dunfee" wrote:

I am attempting to use an array of arrays (ragged arrays), but am having
problems.

It seems the master-array elements must set equal to the sub-arrays only
AFTER the values are entered for the sub-arrays. Then it seems the values of
the elements are set and cannot be changed. Here is my sample
code with comments (my next question follows);

'==================
Sub ABC()
Dim vArr1(1 To 10) As Variant
Dim vArr2(1 To 5) As Variant
Dim vMainArr(1 To 2) As Variant
Dim lCounter As Long
Dim s As String, i As Long, j As Long

' Populate the sub arrays:
For lCounter = 1 To 10
vArr1(lCounter) = lCounter
If lCounter < 6 Then _
vArr2(lCounter) = lCounter * 2
Next lCounter

' Assign the sub arrays to the main array:
' Note that if this isn't done after are populated with values, the array
will be empty.
vMainArr(1) = vArr1
vMainArr(2) = vArr2

'change one of the elements as a test
vArr2(2) = 100 ' note that this change WON'T show in the vMainArr array

' Show the results
s = ""
For i = 1 To 2
For j = LBound(vMainArr(i), 1) To UBound(vMainArr(i), 1)
s = s & vMainArr(i)(j) & ","
Next j
s = s & vbNewLine
Next i
MsgBox s

End Sub

'==================

If my issues above are true, then I need to figure out a way around these
limitations. Note that my actual program would have perhaps 200 elements in
the main array, and 20 sub elements.of mixed type (strings and numbers), then
about 5 of the sub-sub elements would be 1-dimensional arrays. Just using 200
separate arrays would not be desirable.

Perhaps I must repeat the lines where sub arrays are assigned to the main
array EVERY time I change a value.

Any other suggestions?

Joe Dunfee


George Nicholson

Changing values of an array of arrays
 
Or, maybe add the 2 sub arrays to a collection object (which I'm fairly sure
does hold a pointer to vArr2) rather than another array (which just creates
a disconnected copy of the array at that point in time).

HTH,

"Tom Ogilvy" wrote in message
...
You are correct that vArr1 and vArr2 have no relation to the array of
arrays.
The MainArr(2) does not hold a pointer to vArr2. So if you want to
change
the value in the MainArr(2), second position, then do it directly

'==================
Sub ABC()
Dim vArr1(1 To 10) As Variant
Dim vArr2(1 To 5) As Variant
Dim vMainArr(1 To 2) As Variant
Dim lCounter As Long
Dim s As String, i As Long, j As Long

' Populate the sub arrays:
For lCounter = 1 To 10
vArr1(lCounter) = lCounter
If lCounter < 6 Then _
vArr2(lCounter) = lCounter * 2
Next lCounter

vMainArr(1) = vArr1
vMainArr(2) = vArr2

'change one of the elements as a test
vMainArr(2)(2) = 100

' Show the results
s = ""
For i = 1 To 2
For j = LBound(vMainArr(i), 1) To UBound(vMainArr(i), 1)
s = s & vMainArr(i)(j) & ","
Next j
s = s & vbNewLine
Next i
MsgBox s

End Sub

worked for me.

--
Regards,
Tom Ogilvy


"Joe Dunfee" wrote:

I am attempting to use an array of arrays (ragged arrays), but am having
problems.

It seems the master-array elements must set equal to the sub-arrays only
AFTER the values are entered for the sub-arrays. Then it seems the values
of
the elements are set and cannot be changed. Here is my sample
code with comments (my next question follows);

'==================
Sub ABC()
Dim vArr1(1 To 10) As Variant
Dim vArr2(1 To 5) As Variant
Dim vMainArr(1 To 2) As Variant
Dim lCounter As Long
Dim s As String, i As Long, j As Long

' Populate the sub arrays:
For lCounter = 1 To 10
vArr1(lCounter) = lCounter
If lCounter < 6 Then _
vArr2(lCounter) = lCounter * 2
Next lCounter

' Assign the sub arrays to the main array:
' Note that if this isn't done after are populated with values, the array
will be empty.
vMainArr(1) = vArr1
vMainArr(2) = vArr2

'change one of the elements as a test
vArr2(2) = 100 ' note that this change WON'T show in the vMainArr array

' Show the results
s = ""
For i = 1 To 2
For j = LBound(vMainArr(i), 1) To UBound(vMainArr(i), 1)
s = s & vMainArr(i)(j) & ","
Next j
s = s & vbNewLine
Next i
MsgBox s

End Sub

'==================

If my issues above are true, then I need to figure out a way around these
limitations. Note that my actual program would have perhaps 200 elements
in
the main array, and 20 sub elements.of mixed type (strings and numbers),
then
about 5 of the sub-sub elements would be 1-dimensional arrays. Just using
200
separate arrays would not be desirable.

Perhaps I must repeat the lines where sub arrays are assigned to the main
array EVERY time I change a value.

Any other suggestions?

Joe Dunfee





All times are GMT +1. The time now is 01:22 PM.

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