【发布时间】:2014-07-22 08:46:10
【问题描述】:
我在 Excel 2013 的工作簿中有 VBA 函数,它根据泊松分布计算 p 值。当下面代码中的 events 变量超过 1029 时,我得到 运行时错误 '1004': Unable to get the Combin property of the WorksheetFunction class。只要 events1 和 events2 的总和保持在 1029 或以下,就没有问题并且宏可以正常执行。
有没有人知道如何让它在更高的数字下正常运行?非常感谢任何指导!
Sub poisson_meansB()
Dim events1 As Long
Dim events2 As Long
Dim days1 As Long
Dim days2 As Long
events1 = Sheet1.Range("B6").Value
events2 = Sheet1.Range("C6").Value
days1 = Sheet1.Range("B7").Value
days2 = Sheet1.Range("C7").Value
If events2 > 0 Then
events = events1 + events2
p_c = days1 / (days1 + days2)
p_lo = 0
p_hi = 0
For i = 0 To events1
poisson_p_value_term = Application.WorksheetFunction.Combin(events, i) * Application.WorksheetFunction.Power(p_c, i) * Application.WorksheetFunction.Power(1 - p_c, events - i)
p_lo = p_lo + poisson_p_value_term
Next i
For i = events1 To events
poisson_p_value_term = Application.WorksheetFunction.Combin(events, i) * Application.WorksheetFunction.Power(p_c, i) * Application.WorksheetFunction.Power(1 - p_c, events - i)
p_hi = p_hi + poisson_p_value_term
Next i
p = Application.WorksheetFunction.Min(2 * p_lo, 2 * p_hi)
Sheet1.Range("C13") = p
Else
Sheet1.Range("C13") = "-"
End If
End Sub
【问题讨论】:
-
Excel 中允许的最大数字是:
1.79769313486231E+308。你知道你的计算结果是否会大于这个数字吗?如果是这样,那就是导致错误的原因。 -
谢谢,但是即使我将 events1 设置为 1 并将 events2 设置为 1029,COMBIN 函数的结果是 1030,我仍然得到错误。将其更改为 events1 = 1 和 events2 = 1028 COMBIN 函数的结果是 1029 并且不会发生错误。所以这似乎不是问题。
-
没关系,这是导致问题的第二个 COMBIN 函数,您是正确的,因为它已达到上限。从 COMBIN(1030,500) 到 COMBIN(1030,530),结果超过 1.797E+308。组合(1030,499) = 1.7E+308 组合(1030,531) = 1.7E+308