Data Types help ... Overflow errors

L

Larry Levinson

I have a formula to calculate a percentage,

dim used, guests as Long, result as single
result = (USED / GUESTS)*100

and get the dreaded overflow on data type error on some of the
answers.

I tried double, variant and decimal in the dim statment. decimal
wouldn't even take in the dim.

then, I tried, result = round((USED / GUESTS)*100,1)

but the syntax gets hung up on the parentheses ...

help greatly appreciated.


Larry Levinson
Talking up to the vocal ...
LLevinson*Bloomberg.net
(remove the star etc ....)
 
P

papou

Hello Larry
Since one of your variables (guests) used in your calculation is declared as
Long (that's between -2 147 483 648 and 2 147 483 647) I can hardly see this
working because the "result" variable is declared for a lesser value?
Try and declare "result" as long?
HTH
Cordially
Pascal
 
S

Sharad

Does used or guests exceed figure 2,147,483,647
(i.e. two billions and something)?
If not then
can result exceed E+38 or can be less than E-45?

If not then can guests be zero (0)?

Change the code as under and try:-

Dim used, guests As Long, result As Single
If geusts = 0 Then
MsgBox "Zero guests will make it divide by zero _
and cause overflow!"
Exit Sub
End If
result = (used / guests) * 100


Sharad
 
L

Larry Levinson

Overflow ...

papou said:
Hello Larry
Since one of your variables (guests) used in your calculation is declared as
Long (that's between -2 147 483 648 and 2 147 483 647) I can hardly see this
working because the "result" variable is declared for a lesser value?
Try and declare "result" as long?
HTH
Cordially
Pascal

Larry Levinson
Talking up to the vocal ...
LLevinson*Bloomberg.net
(remove the star etc ....)
 
L

Larry Levinson

In case anyone finds this, it wasn't actually an overflow error. it
was a divide by zero errror masking as an overflow. thanks.




Larry Levinson said:
Overflow ...



Larry Levinson
Talking up to the vocal ...
LLevinson*Bloomberg.net
(remove the star etc ....)

Larry Levinson
Talking up to the vocal ...
LLevinson*Bloomberg.net
(remove the star etc ....)
 

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