Hi, would anybody know how to get the results/data of my function below (please see below). I know this can derived through output test range, but I'm not too sure how this could be coded in VBA.
- I have three matrix here, Tot1, Tot2, To3 (see image attached). I need to add matrix Tot1+Tot2 by using the function "Append_Right(ParamArray Matrix())"; and similarly use the same function for "Append_Down(ParamArray Matrix())" to add the matrix Tot1+Tot3. I have defined the function code, but I'm not sure how will I get the results of the data.
For Append right, I need to set the data results in "C28" (should be 12 rows and 16 cols): for Append down, data needs to be in "C43" (should be 16 rows and 12 cols).
here's my function:
- I have three matrix here, Tot1, Tot2, To3 (see image attached). I need to add matrix Tot1+Tot2 by using the function "Append_Right(ParamArray Matrix())"; and similarly use the same function for "Append_Down(ParamArray Matrix())" to add the matrix Tot1+Tot3. I have defined the function code, but I'm not sure how will I get the results of the data.
For Append right, I need to set the data results in "C28" (should be 12 rows and 16 cols): for Append down, data needs to be in "C43" (should be 16 rows and 12 cols).
here's my function:
VBA Code:
Function Append_Right(MatrixParamArray())
'Append ParamArray Right
Tot1 = Worksheets("Sheet2").Range("C3").Resize(12, 12)
Tot2 = Worksheets("Sheet2").Range("P3").Resize(12, 4)
Tot3 = Worksheets("Sheet2").Range("C17").Resize(2, 12)
Dim N_Matrix, Tot1, Tot2
N_Matrix = UBound(Matrix) + 1
Tot1 = 0: Tot2 = 0
For i = 1 To N_Matrix
If UBound(Matrix(0), 1) = UBound(Matrix(i - 1), 1) Then
Tot1 = Tot1
Tot2 = Tot2 + UBound(Matrix(i - 1), 2)
Else
Tot1 = Tot1 + 1
Tot2 = Tot2 + UBound(Matrix(i - 1), 2)
End If
Next i
Dim Result
If Tot1 = 0 Then
ReDim Result(1 To UBound(Matrix(0), 1), 1 To Tot2)
P = 0
For k = 1 To N_Matrix
For j = 1 To UBound(Matrix(k - 1), 2)
For i = 1 To UBound(Matrix(k - 1), 1)
Result(i, P + j) = Matrix(k - 1)(i, j)
Next i
Next j
P = P + UBound(Matrix(k - 1), 2)
Next k
End If
Append_Right = Result
End Function
End Sub
Function Append_Down(ParamArray Matrix())
'Append ParamArray Down
Dim N_Matrix, Tot1, Tot2
N_Matrix = UBound(Matrix) + 1
Tot1 = 0: Tot2 = 0
For i = 1 To N_Matrix
If UBound(Matrix(0), 2) = UBound(Matrix(i - 1), 2) Then
Tot1 = Tot1
Tot2 = Tot2 + UBound(Matrix(i - 1), 1)
Else
Tot1 = Tot1 + 1
Tot2 = Tot2 + UBound(Matrix(i - 1), 1)
End If
Next i
Dim Result
If Tot1 = 0 Then
ReDim Result(1 To Tot2, 1 To UBound(Matrix(0), 2))
P = 0
For k = 1 To N_Matrix
For i = 1 To UBound(Matrix(k - 1), 1)
For j = 1 To UBound(Matrix(k - 1), 2)
Result(P + i, j) = Matrix(k - 1)(i, j)
Next j
Next i
P = P + UBound(Matrix(k - 1), 1)
Next k
End If
Append_Down = Result
End Function
Last edited by a moderator: