OK. I think you may simply not be assigning the arrays to y
correctly. As I mentioned in the comments:
y(k) = x(i,j)
is ONLY storing the value represented by i/j coordinates in the x
array.
In order for y(k)
to refer to the entire array x
at that time, you would do:
y(k) = x
If that is still not functioning, then perhaps something else is incorrect. Here is an example where I have a 2x2 (base 0) array called baseArray
, which I explicitly fill with some values. I iterate from i = 0 to itLimit
, and within each iteration, I iterate over the items in the array, multiplying the values by 2, and storing in an array variable tmpArray
. After the values have been transformed, I store them in arrContainer
, and proceed to the next For i = 0 to itLimit
iteration.
Sub FunWithArrays()
Dim itLimit As Integer '## Iteration limit.'
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim baseArray(2, 2) As Variant '## an example array.'
'## Put some dummy data in this array.'
baseArray(0, 0) = 1
baseArray(0, 1) = 65
baseArray(0, 2) = 13
baseArray(1, 0) = 14
baseArray(1, 1) = 29
baseArray(1, 2) = 44
baseArray(2, 0) = 9
baseArray(2, 1) = 16
baseArray(2, 2) = 33
Dim tmpArray(2, 2) As Variant '## This will temporarily hold values as we transform them.'
Dim arrContainer() As Variant '## an array of arrays, to store the iteration arrays.'
itLimit = 2 '## set a max iteration.'
For i = 0 To itLimit
'## Transform the base array somehow'
For j = LBound(baseArray, 1) To UBound(baseArray, 1)
For k = LBound(baseArray, 2) To UBound(baseArray, 2)
tmpArray(j, k) = baseArray(j, k) * 2
Next
Next
ReDim Preserve arrContainer(i)
arrContainer(i) = tmpArray
Next
Dim y As Variant
'## Now, refer to a single iteration stored in the arrContainer variable:
y = arrContainer(2)
End Sub
In this first screenshot, I use the Locals
window to review the variables and what they contain. After the first iteration, you can see that tmpArray
has been populated, and is the same dimensions as baseArray
, but the values therein have been multiplied by 2.
Examining the arrContainer
variable, we see that it has only 1 item, and that item is an array equal to the tmpArray
that we created in the iteration, above.
After the last iteration, we can review arrContainer
and see that it contains 3 items (0 to 2, per our For i to itLimit
loop). Each array inside arrContainer
is equal to one of the arrays created in the iteration above.
We can now refer to these items, like:
Dim y as Variant
y = arrContainer(2) '# or any in bounds index could be used instead of "2"'
And finally using the VBE Locals window to review y
: