Hi
I have written the below function for which I would like to make the number of array arguments optional. In the current case it takes 5 arrays of equal length as argument, and combines them into the StockMatrix array.
Function SMatrix_C(arr1 As Variant, arr2 As Variant, arr3 As Variant, arr4 As Variant, arr5 As Variant) As Variant
Dim StockMatrix() As Variant
Dim RateMatrix() As Variant
Dim arrResult() As Variant
Dim i As Long, j As Long
Dim h As Long
StockMatrix = Union(arr1, arr2, arr3, arr4, arr5).Value
.
.
.
End Function
How can I make the number of required arrays arbitrary, so that the function may also have worked if I called the function with 7 arrays: SMatrix_C(arr1, arr2, arr3, arr4, arr5, arr6, arr7). I don't know if it makes a difference, but the actual arrays I supply as arguments are worksheet ranges, so that an example call with five array arguments will look like =SMatrix_C(B2:B523;C2:C523;D2:D523;E2:E523;F2:F523)
The full function code is supplied below, however with a few calls to other functions; the below code is irrelevant, and only included for completeness.
Function SMatrix_C(arr1 As Variant, arr2 As Variant, arr3 As Variant, arr4 As Variant, arr5 As Variant) As Variant
Dim StockMatrix() As Variant
Dim RateMatrix() As Variant
Dim arrResult() As Variant
Dim i As Long, j As Long
Dim h As Long
StockMatrix = Union(arr1, arr2, arr3, arr4, arr5).Value
w = UBound(StockMatrix, 2) ' - LBound(StockMatrix, 2) + 1
h = UBound(StockMatrix, 1) ' - LBound(StockMatrix, 1) + 1
ReDim RateMatrix(1 To h - 1, 1 To w)
With Application.WorksheetFunction
For i = 1 To w
R_SM = RateContinuous(.Index(StockMatrix, 0, i))
For j = 1 To UBound(R_SM)
what = UBound(R_SM)
RateMatrix(j, i) = R_SM(j)
Next j
Next i
ReDim arrResult(1 To w, 1 To w)
For i = 1 To w
For j = 1 To w
arrResult(i, j) = .Covar(.Index(RateMatrix, 0, i), .Index(RateMatrix, 0, j))
Next j
Next i
End With
SMatrix_C = arrResult
End Function
Bookmarks