I am writing an Excel VBA function that returns an array to use in other functions. When I test out the function GenerateBlendedReturnSeries in my Excel Sheet and use ctlr-shift-enter to see the full result, the entire array is all zeros. Strangely, however, when I check Debug.Print (BlendedReturnSeriesArray(300, 1)), the proper non-zero value is returned. Why is the function unable to properly return this result? I array being returned is 329 x 1 and contains "" values in rows when the multiplication/addition in the for loop returns an error.
Function GenerateBlendedReturnSeries(AccountID1 As String, Account1Proportion As Double, _
Optional ByVal AccountID2 As String, Optional ByVal Account2Proportion As Double, _
Optional ByVal AccountID3 As String, Optional ByVal Account3Proportion As Double) As Variant 'Vs. As Double()
' CODE IN BETWEEN
Dim BlendedReturnSeriesArray As Variant
ReDim BlendedReturnSeriesArray(ArraySize, 1)
Debug.Print (ArraySize)
On Error Resume Next
For i = 0 To UBound(BlendedReturnSeriesArray)
BlendedReturnSeriesArray(i, 1) = _
Account1PeriodReturnSeriesArray(i, 1) * Account1Proportion _
+ Account2PeriodReturnSeriesArray(i, 1) * Account2Proportion _
+ Account3PeriodReturnSeriesArray(i, 1) * Account3Proportion
'Debug.Print (BlendedReturnSeriesArray(i, 1))
'Debug.Print (i)
Next i
On Error GoTo 0
Debug.Print (BlendedReturnSeriesArray(300, 1))
GenerateBlendedReturnSeries = BlendedReturnSeriesArray 'BlendedReturnSeriesArray
End Function
ArraySizecome from? OrAccount1PeriodReturnSeriesArrayetc ?ArraySizecomes from theRows.Countof the Range that you get from anIndex,Matchusing theAccountID’s.Account1PeriodReturnSeriesArrayis aVariantof dimensionsArraySizex 1 that comes from the RangeAccount1PeriodReturnsSeries, which you get from anIndex,Matchusing theAccountID’s. I omitted this code since as theDebug.Printshows, the final array is working as expected except when I try to return it.