【问题标题】:Excel VBA #Value! ErrorExcel VBA #价值!错误
【发布时间】:2016-03-01 10:21:21
【问题描述】:

我有以下功能,当我运行它时会显示#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

Bootstrapping 函数计算以下值 事实上我正在尝试计算这个公式 Q(t,Tn)=(∑(j=1)to(n-1) Z(t,Tj)[LQ(t,Tj-1)-(L+dtSn)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

【问题讨论】:

  • 请逐步解释您要进行的计算,因为这非常黑暗,您可以编辑您的帖子以包含此内容并帮助我们帮助您...因为现在,我我真的在黑暗中拍摄!
  • @R3uK。我已经编辑并给出了 Bootstrapping 函数的公式。我希望它现在不黑。

标签: vba excel syntax-error udf


【解决方案1】:

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


  1. 您需要键入函数的输出(在函数名称的行上)
  2. P = Z(j) * (L*Q(j-1)-(L+ dt * a(n) * Q(j)) 行中缺少括号
  3. 当您尝试重新调整数组时,n 为空(abQ 也是如此),因此您需要定义它们!
  4. 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

【讨论】:

  • 我再次尝试通过输入 n=2 来运行代码,但它显示了相同的#value!错误。
  • 在 Excel Spreadhseet 输出单元格 D1 中,我输入 =Bootstrap(A1:A2,B1:B2,0.4) ,生成#value!错误。
  • 问题在于您的计算以及您使用空变量的事实(这里,abQ 是空的,因此您只需擦除您选择的单元格的内容并用空变量计算P,所以实际上计算是P = 0 - L = - L。另见第4点
  • 您能描述一下您要做什么吗?或者更正循环中的计算算法,因为卷轴问题就在这里! ;)
  • 我已将 Z(j) 替换为 Z.Cells(j,2).Value 但没有效果,我的输入是 Z 列矩阵,我已将 Z 中的值存储到 b 矩阵中,类似地S 到 a() 。因此,a & b 不应为空。 Q 的初始值为 0,即 Q(0) 但对于值来说 j= 2 ,将需要 Q(2) ,但不存在。我正在更正 Q(j) 的计算部分并进行检查。
【解决方案2】:

试试这个代码:输入为 =Bootstrap(A1:B1,A2:B2,0.4)
我已更正以下内容
- 将范围分配给变体
- 将 dt 定义为双精度
- 将 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

【讨论】:

  • 谢谢查尔斯。这给出了一些输出值。这似乎是正确的。我正在查。但是如果 Inputs 在 Columns 而不是 Rows 呢?然后只需更改 Application .WorkshhetFunction.Max(S.Rows.Count,Z.Rows.Count) ?
  • 然后使用 Rows.Count 代替 Columns.Count 并反转 a() 和 b() 引用中的索引
猜你喜欢
  • 2015-08-27
  • 2017-04-15
  • 2015-06-13
  • 1970-01-01
  • 1970-01-01
  • 2022-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多