Juggler_IN
Active Member
- Joined
- Nov 19, 2014
- Messages
- 358
- Office Version
- 2003 or older
- Platform
- Windows
I have code to print a 3d array as 2d array to a range. With the sample test array the output is:
But, my requirement is:
I have not been able to figure out the modification. Any help is appreciated.
1 | 3 | 5 |
7 | 9 | 11 |
13 | 15 | 17 |
2 | 4 | 6 |
8 | 10 | 12 |
14 | 16 | 18 |
But, my requirement is:
1 | 2 | 3 |
4 | 5 | 6 |
7 | 8 | 9 |
10 | 11 | 12 |
13 | 14 | 15 |
16 | 17 | 18 |
I have not been able to figure out the modification. Any help is appreciated.
VBA Code:
Sub Print3DArrayToRange(arr As Variant, ws As Worksheet, startCell As Range)
Dim x As Long, y As Long, z As Long
Dim rowOffset As Long
For z = LBound(arr, 3) To UBound(arr, 3)
rowOffset = (z - LBound(arr, 3)) * (UBound(arr, 1) - LBound(arr, 1) + 1)
For y = LBound(arr, 2) To UBound(arr, 2)
For x = LBound(arr, 1) To UBound(arr, 1)
ws.Cells(startCell.Row + rowOffset + x - LBound(arr, 1), startCell.Column + y - LBound(arr, 2)) = arr(x, y, z)
Next x
Next y
Next z
End Sub
Sub Test()
ReDim x(2&, 2&, 1&) As Integer
x(0&, 0&, 0&) = 1&
x(0&, 0&, 1&) = 2&
x(0&, 1&, 0&) = 3&
x(0&, 1&, 1&) = 4&
x(0&, 2&, 0&) = 5&
x(0&, 2&, 1&) = 6&
' ---
x(1&, 0&, 0&) = 7&
x(1&, 0&, 1&) = 8&
x(1&, 1&, 0&) = 9&
x(1&, 1&, 1&) = 10&
x(1&, 2&, 0&) = 11&
x(1&, 2&, 1&) = 12&
' ---
x(2&, 0&, 0&) = 13&
x(2&, 0&, 1&) = 14&
x(2&, 1&, 0&) = 15&
x(2&, 1&, 1&) = 16&
x(2&, 2&, 0&) = 17&
x(2&, 2&, 1&) = 18&
Print3DArrayToRange x, Sheets("Output"), Range("L15")
End Sub