Puzzled with VBA behavior: &H100 * &HBB
generates an overflow error (Runtime Error '6' Overflow
), while 16 ^ 2 * 187
does not. Suspect it's data-type coercion with the &H
literal character; just was cleaner to read in the code using hex notation (color settings). Would like to understand what's behind it, if you can shed some light!!
MS Word VBA (VBA7) on 64-bit Office365 license.
CodePudding user response:
It's because &H100
and &HBB
default to Integer which cannot hold the expected result: 47872
.
However, 16 ^ 2
defaults to Double and then 187
as well, and Double can easily hold 47872
.
Convert to Long (or Currency or Decimal) when you expect larger integer results:
? CLng(&H100) * CLng(&HBB)
47872