Overflow when multiplying Integers and assigning to Long
This is a really odd VBA quirk. I'm amazed I've never bumped into this.
So it looks like the
Your literals 24, 60, and 60 are all of type Integer by default, so your However, the literal 32,768 in the third example above defaults to Long type (since it is too large to be an Integer) and so the The help file also says this:
Emphasis mine. Now this little rule sounds like common sense, and anyone would reasonably assume that it applies in your case. But your numbers are of type Integer, not Variant/Integer, so VBA doesn't apply this rule! Makes absolutely no sense to me, but that's how it is, and that's what the documentation says. Solution: make one of the arguments of your
In fact, and this is probably why I've never bumped into this quirk, I make a habit of declaring all of my Integer variables as Long instead, unless there is a specific concern that having Longs instead of Integers will cause problems with memory use or execution time (which is almost never the case). Granted, this won't help in cases when you operate on literals smaller than 32,768, because they default to Integer type. In your comment, you ask what a Variant/Integer is. Variant is basically a container type for any other data type. In the particular case where you make it contain an Integer:
But as noted above, a plain old Integer triggers the overflow error:
| |||||||
|
'메모' 카테고리의 다른 글
vba chr chrw 등 문자 함수...펌 (0) | 2013.08.21 |
---|---|
excel 예측 함수 forecast등...펌 (0) | 2013.08.16 |
옵티머스G프로만 usb호스트됨. G이하는 안되는듯...펌 (0) | 2013.08.13 |
strsplit 분리자 정규식에서 escape backslash가 두개인 이유...펌 (0) | 2013.08.12 |
vba checkbox name, value, checkbox_change()...펌 (0) | 2013.08.03 |
^
operator returns a Double, which sidesteps the problem described in my answer below. – Jean-François Corbett Jun 26 at 10:38