在Excel中返回一个数组的VBA函数
你好,我希望你能帮助我这个。 我写了一个简单的函数来将回报转化为价格。 这是没用的,但我会用它来解释我的问题。 该function起作用。 它做我想要的。
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相同的方式。 任何build议?
非常感谢您的宝贵时间
VBA中的数组是基于0的,所以:
ReDim prices(1 To (UBound(temp, 1) + 1), 1)
相当于
ReDim prices(1 To (UBound(temp, 1) + 1), 0 To 1)
有问题的代码返回了预期的结果,但在结果数组的第二列。 将第二维的下界更改为1可以解决问题。