Error with a boolean operation
Will your numbers always have 4 decimal places or less? If so, change your
function declaration to this...
Public Function MY_tester(ByVal z, ByVal w)
The ByVal's make sure the original values passed in will not be changed by
what I propose next. Add these two lines immediately after the function
declaration...
z = CCur(z)
w = CCur(w)
Your code should now work. If your values can have more than 4 decimal
places, then this **might** work (well, it will for for you posted example,
but it might not work depending on the size of your numbers and whether you
use mathematical operations, such as Sqr, Sin, etc., on those values)...
z = CDec(z)
w = CDec(w)
Rick
"Bob" wrote in message
...
Thanks guys for the insight into the problem. Then I guess, there is no
way of knowing if z+w is exactly an integer or not? The reason I need to
know this is that I am evaluating a function that works for all numbers
except exact integers. Even very close to integers will work, but not the
integer itself.
Bob
"Bob" wrote in message
...
Hi everyone:
In excel VBA, I have the following code:
Public Function MY_tester(z, w)
On Error GoTo ErrHandler
If Not IsNumeric(z) Then
MY_tester = "Error: Z must be a number"
ElseIf Not IsNumeric(w) Then
MY_tester = "Error: w must be a number"
ElseIf ((z + w) < 0) And ((z + w) = Fix(z + w)) Then
MY_tester = "Error: z+w cannot be zero nor negative integers"
Else
MY_tester = "Answer OK"
End If
ErrHandler:
If Err Then
MY_tester = "Error: " & Err.Description
End If
End Function
Public Sub MY_SUB()
MsgBox MY_tester(-4.4, 3.4)
End Sub
When I run the sub MY_SUB, the program returns false for ((z + w) = Fix(z
+ w)) , even though it is true (after all, -1 is equal to -1). Does
anyone know why? I think this may be a bug in VBA. I appreciate all
your help.
Bob
|