簡體   English   中英

雖然在Debug.Print中可以看到函數無法正確返回數組

[英]Function Not Properly Returning Array Though Can See in Debug.Print

我正在寫一個Excel VBA函數,該函數返回一個數組,供其他函數使用。 當我在Excel工作表中測試函數GenerateBlendedReturnSeries並使用ctlr-shift-enter查看完整結果時,整個數組全為零。 但是,奇怪的是,當我檢查Debug.Print (BlendedReturnSeriesArray(300, 1)) ,返回了正確的非零值。 為什么函數無法正確返回此結果? 我返回的數組是329 x 1,並且在for loop的乘法/加法返回錯誤時,行中包含""值。

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

除非你這樣做

ReDim BlendedReturnSeriesArray(1 To ArraySize, 1 To 1)

或者您正在使用Option Base 1 (絕不是一個好主意),則返回數組的大小將設置為(0到ArraySize,0到1),因此很可能只看到該數組的第一個“列”(零索引)。 如果將公式擴展為涵蓋兩列,則會看到丟失的數字。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM