score:4

Accepted answer

the error is on 362 * 100. it is considered integer and it is up to 32767.

try like this:

public sub testme()    
    dim pct as double
    pct = (cdbl(362) * 100) / 2005        
end sub

the problem is that if you have any of the following 4 mathematic expressions (+-/*) with values under 2^15-1 or 32767, vba considers them integer and the result is tried to be parsed to integer as well. (the integer values are actually from -32768 to 32767)

thus, 32767+1 gives an overthrow error. if you try with 32768+1 it will be ok, because 32768 will be parsed to long.

for the power operator, ^, vba parses to double and it is quite ok - 2 ^ 10 + 32000

score:3

because the integer type in vba is 16 bits signed, and 36200 overflows its maximum value of 32767. you can avoid this by appending & to one of the operands to make it a long literal, for example (362& * 100) / 2005.


Related Query