E
ExcelMonkey
I have a transpose function I am using on a 2D array. I am setting up the
array, printing output to immediate window, transposing array, printer
revised output to Immediate window. The results are not making sense to me.
Why is the output the same after I transpose the array.
Expected Output in Immediate Window:
Fred
10
Fred
Jack
Joe
Actual Output in Immediate Window:
Fred
10
Fred
10
Sub MakeSenseOfArrays()
Dim Array1 As Variant
ReDim Array1(0 To 1, 0 To 2)
'2D Array 2rowsX3columns
'Should look like this
'Fred Jack Joe
'10 20 30
Array1(0, 0) = "Fred"
Array1(0, 1) = "Jack"
Array1(0, 2) = "Joe"
Array1(1, 0) = 10
Array1(1, 1) = 20
Array1(1, 2) = 30
For X = 0 To UBound(Array1, 1)
'Print Rows of fist column
Debug.Print Array1(X, 0)
Next
Transpose2D (Array1)
'Should now be a 2D Array of 3rowsX2columns
'Should look like this
'Fred 10
'Jack 20
'Joe 30
Debug.Print ""
For X = 0 To UBound(Array1, 1)
Debug.Print Array1(X, 0)
Next
End Sub
***********************************
Function Transpose2D(vaData) As Variant
'Transpose the input array swapping rows for columns
Dim i As Long, j As Long
Dim vaTransposed As Variant
ReDim vaTransposed(LBound(vaData, 2) To UBound(vaData, 2), _
LBound(vaData, 1) To UBound(vaData, 1)) As Variant
For i = LBound(vaData, 1) To UBound(vaData, 1)
For j = LBound(vaData, 2) To UBound(vaData, 2)
vaTransposed(j, i) = vaData(i, j)
Next j
Next i
Transpose2D = vaTransposed
End Function
Thanks
EM
array, printing output to immediate window, transposing array, printer
revised output to Immediate window. The results are not making sense to me.
Why is the output the same after I transpose the array.
Expected Output in Immediate Window:
Fred
10
Fred
Jack
Joe
Actual Output in Immediate Window:
Fred
10
Fred
10
Sub MakeSenseOfArrays()
Dim Array1 As Variant
ReDim Array1(0 To 1, 0 To 2)
'2D Array 2rowsX3columns
'Should look like this
'Fred Jack Joe
'10 20 30
Array1(0, 0) = "Fred"
Array1(0, 1) = "Jack"
Array1(0, 2) = "Joe"
Array1(1, 0) = 10
Array1(1, 1) = 20
Array1(1, 2) = 30
For X = 0 To UBound(Array1, 1)
'Print Rows of fist column
Debug.Print Array1(X, 0)
Next
Transpose2D (Array1)
'Should now be a 2D Array of 3rowsX2columns
'Should look like this
'Fred 10
'Jack 20
'Joe 30
Debug.Print ""
For X = 0 To UBound(Array1, 1)
Debug.Print Array1(X, 0)
Next
End Sub
***********************************
Function Transpose2D(vaData) As Variant
'Transpose the input array swapping rows for columns
Dim i As Long, j As Long
Dim vaTransposed As Variant
ReDim vaTransposed(LBound(vaData, 2) To UBound(vaData, 2), _
LBound(vaData, 1) To UBound(vaData, 1)) As Variant
For i = LBound(vaData, 1) To UBound(vaData, 1)
For j = LBound(vaData, 2) To UBound(vaData, 2)
vaTransposed(j, i) = vaData(i, j)
Next j
Next i
Transpose2D = vaTransposed
End Function
Thanks
EM