簡體   English   中英

在Excel中返回數組的VBA函數

[英]VBA function that returns an array in excel

大家好,我希望你能幫我這個忙。 我寫了一個簡單的函數將收益轉換為價格。 它沒有用,但是我將用它來解釋我的問題。 該功能有效。 它可以滿足我的要求。

Function ret2prices(ByVal r As Range)

    Dim temp() As Variant
    temp = r

    Dim prices() As Variant
    ReDim prices(1 To (UBound(temp, 1) + 1), 1)
    prices(1, 1) = 1

    For i = 2 To UBound(prices, 1)
        prices(i, 1) = 1 + temp(i - 1, 1)
        prices(i, 1) = prices(i - 1, 1) * prices(i, 1)
    Next i

    ret2prices = prices

End Function

問題是,當我在excel工作表中使用它時,它總是返回0。我希望能夠以與CTRL + SHIFT + ENTER一起使用MMULT的方式來使用它。 有什么建議嗎?

非常感謝您的寶貴時間

VBA中的數組基於0,因此:

ReDim prices(1 To (UBound(temp, 1) + 1), 1)

相當於

ReDim prices(1 To (UBound(temp, 1) + 1), 0 To 1)

有問題的代碼返回了預期結果,但返回結果數組的第二列。 將第二維的下限更改為1可解決此問題。

暫無
暫無

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

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