Macro erro

O

orquidea

Hi

I have the following subprocedure
Per=(our20/total20)
Some times the variables our20 and total20 will be =0. In these situations,
I am getting the message "Run-time error'6' overflow" . The subprocedure
above is highlighted. I believe it is because the division is 0/0.

Could anyone help me to solve this problem?

Thanks,
Orquidea
 
F

FSt1

hi
you could wrap it in an if statement to test for zero.
if our20/total20<> 0 then
per=(our20/total20)
end if
or....
if our20/total20= 0 then
do something else
else
per=(our20/total20)
end if

my thoughts
Regards
FSt1
 
O

orquidea

Thanks a lot for your help. It worked.

Orquidea
FSt1 said:
hi
you could wrap it in an if statement to test for zero.
if our20/total20<> 0 then
per=(our20/total20)
end if
or....
if our20/total20= 0 then
do something else
else
per=(our20/total20)
end if

my thoughts
Regards
FSt1
 
R

Rick Rothstein \(MVP - VB\)

I have the following subprocedure
Per=(our20/total20)
Some times the variables our20 and total20 will be =0. In these
situations,
I am getting the message "Run-time error'6' overflow" . The
subprocedure
above is highlighted. I believe it is because the division is 0/0.

Zero divided by any number (except zero) equals zero, so the numerator being
zero is not the problem. On the other hand, any number divided by zero is
(in imprecise mathematical terms) infinity, which is not a number; so, VB
returns an error for division by zero. The special case of zero divided by
zero is undefined, not infinity... the reason (again, in imprecise
mathematical terms) has to do with the previous two statements... any number
divided by zero is infinity but zero divided by any number is zero... there
is no way to decide which of these holds for a result, zero or infinity, so
the division of zero by zero is undefined.

Anyway, the way to handle your problem is to test the denominator for
zero...

If total20 = 0 Then
'
' Handle your error here. There are lots of ways to do this, but they
' all depend on how you want your program to proceed for this case
'
Else
Per = our20 / total 20
End If

Rick
 
R

Rick Rothstein \(MVP - VB\)

It worked??? Are you sure?

Testing our20/total20 in an If statement will generate an error if total20
is equal to 0.

Rick
 
O

orquidea

Here is how I rewrote the procedure based on FSt1's answer.
If Total20 <> 0 Then
per = (our20 / total20)
End If
I tested this and worked. I know when divisions are done in spreadsheet
where a number is divided by Zero, the message "#DIV/0!" pops up. Therefore
I assumed that the error I got with FSt1's answer was because of that.
That's why I rewrote the procedure above.

Thanks for your explanation. It helps me to understand better how macros
should be set.

Orquidea
 
F

FSt1

yes.
I answered to quick on this one. what popped in my mind was "test for zero"
and i didn't think about what i wrote enought. I would have caught if i have
tested but i didn't test which i usually do.
sorry for the miss lead.
regards
FSt1
 

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