October 9th, 2012, 02:07 PM
Originally Posted by couttsj
You cannot divide by zero; it will always produce an error.
That is true, however, the IIf should use the first, not the second condition.
However, this is not the case. In a beautiful example of flummoxing engineering VB actually evaluates both even though it's completely unnecessary to do so!
Originally Posted by VBA HELP
IIf always evaluates both truepart and falsepart, even though it returns only one of them. Because of this, you should watch for undesirable side effects. For example, if evaluating falsepart results in a division by zero error, an error occurs even if expr is True.
Replace the IIf with a traditional If Then Else block instead.
If (s3breakdown * s4breakdown = 0) Then
s5breakdown = 0
s5breakdown = s3breakdown - s4breakdown / s4breakdown
Be sure to pay attention to operator precedence and also understand that this is the same as
s5breakdown = s3breakdown - (s4breakdown / s4breakdown)
s5breakdown = (s3breakdown - s4breakdown) / s4breakdown
THIS IS IMPORTANT: as you have written it, this is also the same as:
s5breakdown = s3breakdown - 1
because n / n always equals 1 for all times when n is not 0, and if n is 0 it's mathematically impossible
“Today you are You, that is truer than true. There is no one alive who is Youer than You.” - Dr. Seuss
Last edited by medialint : October 9th, 2012 at 02:15 PM.