Any help on how to get the output results of the function from VBA codes..

anne2021

New Member
Joined
Dec 4, 2021
Messages
9
Office Version
  1. 2021
Platform
  1. MacOS
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).

Screen Shot 2022-01-24 at 5.42.57 PM-min.png

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:

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.

Forum statistics

Threads
1,214,668
Messages
6,120,825
Members
448,990
Latest member
rohitsomani

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top