[英]Excel VBA Overflows when dividing two Doubles
I'm getting an Overflow error when I try to divide two doubles, and I can't figure out why. 当我尝试将两个双精度数相除时出现了溢出错误,但我不知道为什么。
Here's my code as it is now 这是我现在的代码
'x will be used to sum the elements
Dim x, ct As Double
x = 0
ct = 0
For Each cell In Range(rng)
If cell.Offset(, offset1).Value = Crit1 And cell.Offset(, offset2).Value = Crit2 Then
x = x + cell.Value
ct = ct + 1
End If
Next
'Divide by count
Avg = x / ct
At first, ct
was declared as Long
, but I changed it to Double
to see if that might fix it, but it didn't. 最初, ct
被声明为Long
,但是我将其更改为Double
以查看是否可以解决该问题,但是没有解决。
I've also tried changing the last line to Avg = CDbl(x / ct)
, but I get the same error. 我也尝试将最后一行更改为Avg = CDbl(x / ct)
,但出现相同的错误。
The values in cell.Value
will always be real numbers, usually ranging between 0 and about 9,000,000, occasionally going up as high as 17,000,000. cell.Value
的值将始终是实数,通常在0到大约9,000,000之间,偶尔会高达17,000,000。 The numbers greater than about 20,000 are almost always integers. 大于约20,000的数字几乎总是整数。
It's divide by zero error. 它除以零误差。 VBA just reported it as "overflow". VBA刚刚将其报告为“溢出”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.