为什么细胞(1,1)= 500 * 100会导致溢出,但50000 * 100不会? [英] Why cells(1,1) = 500 * 100 causes overflow but 50000*100 doesn't?
本文介绍了为什么细胞(1,1)= 500 * 100会导致溢出,但50000 * 100不会?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
sub add()
'这将导致溢出错误
cells(1,1)= 500 * 100
'但这不会
单元格(2 ,2)= 50000 * 100
end sub
解决方案
p>考虑:
Sub add()
'这样做:
单元格(1,1) = CLng(500)* 100
'这样做:
单元格(2,2)= 50000 * 100
End Sub
显然,VBA正在为第一个表达式选择默认类型 Integer
,因为该类型足够大以容纳文字在右边。 50000对于 Integer
而言太大,所以它将它解释为一个 Long
。 CLng
明确触发促销到 Long
。
Well, I just created a simple sub and it give the error of overflow. However, I don't see anything wrong with the code, and it is really weird since 50000*100 is much bigger than 500*100.
sub add()
'This will cause an overflow error
cells(1,1) = 500 * 100
'But this won't
cells(2,2) = 50000 * 100
end sub
解决方案
Consider:
Sub add()
'This works:
Cells(1, 1) = CLng(500) * 100
'as does this:
Cells(2, 2) = 50000 * 100
End Sub
Evidently VBA was picking a default type of Integer
for the first expression because that type is large enough to hold the literals on the right hand side. 50000 is too big for an Integer
so it interprets it as a Long
. CLng
explicitly triggers a promotion to Long
.
这篇关于为什么细胞(1,1)= 500 * 100会导致溢出,但50000 * 100不会?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文