0

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
2
  • Where does ArraySize come from? Or Account1PeriodReturnSeriesArray etc ? Commented Oct 19, 2018 at 0:09
  • ArraySize comes from the Rows.Count of the Range that you get from an Index,Match using the AccountID’s. Account1PeriodReturnSeriesArray is a Variant of dimensions ArraySize x 1 that comes from the Range Account1PeriodReturnsSeries, which you get from an Index,Match using the AccountID’s. I omitted this code since as the Debug.Print shows, the final array is working as expected except when I try to return it. Commented Oct 19, 2018 at 2:43

1 Answer 1

1

Unless you do this

ReDim BlendedReturnSeriesArray(1 To ArraySize, 1 To 1)

or you're using Option Base 1 (never a good idea), your return array will be sized as (0 to ArraySize, 0 to 1), so likely you're only seeing the first "column" of the array (the zero index) on your worksheet. If you expand the formula to cover two columns you'll see the numbers you're missing.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.