Help with simple arithmetic

N

ngohwoonhiong

i have the following declaration and piece of code

Dim netProfit As Long
netProfit = (7364 - 2298) * 20


Why does it fail with "runtime error 6" Overflow.

the result should be 101320, should be okay for Long right ?

Any help appreciated.

Thanx
 
J

Jerry W. Lewis

The right hand side involves only integers, so it evaluates as an
integer expression (which overflows). The final result (if it hadn't
overflowed) would then be coerced to long.

To perform the multiplication as a long instead of an integer, at least
one of the numbers must be a long, as in
netProfit = (7364 - 2298) * 20&

Jerry
 
Top