Excel VBA #值!错误

Excel VBA #Value! Error

我有以下功能,当我 运行 它说#value!错误。

如有任何帮助,我将不胜感激。

Function Bootstrap(S As Object, Z As Object, L As Double)

    Dim j As Integer
    Dim a() As Double
    Dim b() As Double
    Dim n As Integer
    Dim Q() As Double
    Dim sum As Double
    Dim P As Double

    ReDim a(1 To n)
    ReDim b(1 To n)
    ReDim Q(1 To n)

    dt = 1
    sum = 0
    Q(0) = 0

    For j = 1 To n - 1
        S.Cells(j, 1).Value = a(j)
        Z.Cells(j, 2).Value = b(j)
        P = Z(j) * (L * Q(j-1) - (L + dt * a(n) * Q(j))
        sum = sum + P
    Next j

    Bootstrap = sum

End Function

自举函数计算出以下值 事实上我正在尝试计算这个公式 Q(t,Tn)=(∑(j=1)to(n-1) Z(t,Tj)[LQ(t,Tj-1)-(L+dt Sn)Q(t,Tj)]/[Z(t,Tn)(L+dt*Sn)]+(Q(t,Tn-1) L)/(L+dtSn)

给定的输入是[S1 ,S2,….Sn ],[Z(t,T1),Z(t,T2)…..Z(t,Tn)]and and L=0.4

养成格式化和递增代码的习惯,尤其是在发布之前!


  1. 您需要键入函数的输出(在函数名称行)
  2. P = Z(j) * (L*Q(j-1)-(L+ dt * a(n) * Q(j))
  3. 中缺少括号
  4. n 为空(abQ 也是空的)当您尝试重新调整数组时,因此您需要定义它们!
  5. Z(j)也会报错,因为是Range,需要Z.Cells(i,j)

试试这个:

Function Bootstrap(S As Range, Z As Range, L As Double) As Double
Dim j As Integer
Dim a() As Double
Dim b() As Double
Dim n As Integer
Dim Q() As Double
Dim sum As Double
Dim P As Double

n = Application.WorksheetFunction.Max(S.Columns.count, Z.Columns.count)
a = S.Value
b = Z.Value
dt = 1
sum = 0
ReDim Q(1 To n)
Q(0) = 0
'Q(1) = "??"


For j = 1 To n - 1
    P = b(1, j) * (L * Q(j - 1)) - (L + dt * a(1, j) * Q(j - 1))
    sum = sum + P
    Q(j) = sum
Next j

Bootstrap = sum
End Function

试试这个代码:输入为 =Bootstrap(A1:B1,A2:B2,0.4)
我已更正以下内容
- 将范围分配给变体
- 将 dt 定义为 double
- 将 Q() 调暗为 0 到 n
- 在公式
中使用 A() 和 b() - 输入范围是行而不是列

Function Bootstrap(S As Range, Z As Range, L As Double) As Double
Dim j As Integer
Dim a As Variant
Dim b As Variant
Dim n As Integer
Dim Q() As Double
Dim sum As Double
Dim P As Double
Dim dt As Double

n = Application.WorksheetFunction.Max(S.Columns.Count, Z.Columns.Count)
a = S.Value
b = Z.Value
dt = 1
sum = 0
ReDim Q(0 To n)
Q(0) = 0

For j = 1 To n - 1
    P = b(1, j) * (L * Q(j - 1)) - (L + dt * a(1, j) * Q(j - 1))
    sum = sum + P
    Q(j) = sum
Next j

Bootstrap = sum
End Function