【问题标题】:VBA: Calculating LN Daily Returns - Subscript out of RangeVBA:计算 LN 每日收益 - 下标超出范围
【发布时间】:2015-08-10 16:58:03
【问题描述】:

我是 VBA 中的数组的新手,这在 MatLab 中要容易得多,但我似乎无法让代码工作。简单来说,我正在计算一个指数的 LN 每日收益:LN(t0 / t1) 下面是代码:

Dim Index() As Variant

Dim Ret() As Variant

    Index = Range("B9:B29")

    For i = 1 To UBound(Index) - 1

        Ret(i, 1) = Log(Index(i, 1) / Index(i + 1, 1))

    Next i

我在 Ret(i,1) 循环中遇到错误。任何帮助将不胜感激!

【问题讨论】:

  • 您必须先ReDim Ret(1 to UBound(Index) - 1, 1 to 1),然后才能向其添加任何值。
  • 谢谢!我现在在同一行出现溢出错误 [Ret(i,1) = Log(index....)

标签: vba excel


【解决方案1】:

找到了,ReDim 时将 Return 变成一维数组。所以 Ret(i,1) 应该是 Ret(1)。我想知道为什么 Index = Range 返回二维数组?

Dim Index() As Variant
    Dim Ret() As Variant


    Index = Range("B9:B29")

    ReDim Ret(1 To UBound(Index) - 1)


    'Debug.Print Index(1, 1)


    For i = 1 To UBound(Index) - 1

        Ret(i) = Log(Index(i, 1) / Index((i + 1), 1))

    Next i

【讨论】:

  • someRange.Value 总是返回一个二维数组,除非someRange 是一个单元格,在这种情况下它只返回那个单元格的值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多