VBA overflow

F

FGM

Office xp windows 200
LO = Log(10)
In vba have a double loop..
R = 0 to 80
K = 0 to 16
When R = 66 get an overflow because of 500 * R
D2(R,K) = S(K) -20 * Log(500*R)/LO -8.69 * A2(K) * 500 * R

Got overflow with above .. also got over flow by just doing 500 *66

Had to solve the problem by
D2(R,K) = S(K)-20 * ((log(500)/LO) * (Log(R)/LO) - A1(K) + 5 * R

Why do I get an overflow with anything over 500* 66 or anything multiplied
greater than 32500?

Thank you for your input... spent a lot of time trying to figure out what
the over flow problem was and how to solve it.

thanks
fgm
 
R

Robert Morley

Are you declaring R specifically at the top of the procedure using Dim? If
not, you should be.

Whether you're not, or whether you're declaring it as an integer, one way or
the other, it thinks it's an integer. The maximum value for an integer is
32767 (or maybe 32768...I forget which one it is for + vs. -).

If you add the following at the top of your procedure, everything should
work just fine after that with either equation:

Dim R As Long

The maximum value of a Long is 2 ^ 31 (possibly 2 ^ 31 - 1, same as above).


Rob
 
R

Robert Morley

D'oh! Just thought about it and the maximum value for an Integer is
obviously 32767.

Similarly, the maximum value for a Long is 2 ^ 31 - 1.


Rob
 
G

George Nicholson

If you are multiplying 2 integers, the compiler seems to assume that the
result will be an Integer. You need to give it a clue that the result might
not fit inside an Integer data type (the Integer range is from -32,767 to
32,767). Making sure you specify that at least one Long Integer is involved
is the easiest way.

? 32767 * 4 ' generates an Overflow error: 2 Integers
? 32768 * 4 ' does not generate an error: 1 LongInteger * 1 Integer

Similarly,
? Clng(500) * 66 'does not generate an error
? 500 * Clng(66) 'does not generate an error

Specifically declaring R, etc., as Long Integers, rather than Integers
should achieve the same effect as using Clng() above.
Dim R as Long
R = 66
Msgbox 500 * R
Will not raise an error.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top