PDA

View Full Version : Strange VBA Operation Outcome



sam314159
05-25-2011, 11:18 AM
Will someone please look at the attachment below and try to explain to me why that's happening?

The value of X is 1.2 yet it's not passing the X <= 1.2 check.

Here's some more info that might help troubleshoot:

The value of X is rounded up earlier in the code, if the original value of X, before rounding, was 1.2 then it would pass the check but when the original value of X is 1.19 and it's rounded up to 1.2 then what you see in the screenshot is happening.

Any ideas?

Kenneth Hobs
05-25-2011, 11:39 AM
Step through the code using F8. I suspect that it is not doing what you think it is in earlier steps.

Of course if you get a value from a cell, cell formatting can make you think that the value is one thing while it may be another.

sam314159
05-25-2011, 11:44 AM
Kenneth, I am confirming the value of X by adding a watch on X and monitoing it.

You can see the watch in the bottom of the screenshot and it does show a value of 1.2

Kenneth Hobs
05-25-2011, 12:37 PM
I can not duplicate your problem. Keep in mind that Double values are not absolute numbers. What rounding are you using?

Try to duplicate your problem for posting purposes by complete example code.

In this code, counter1 is 4 at the end as one would expect.
Dim counter1 As Integer, counter2 As Integer

Sub Test_XTest()
ResetXTest

XTest 1.2
XTest 1.19

XTest 1.2
XTest 1.19
End Sub

Sub XTest(X As Double)
If X <= 1.2 Then
counter1 = counter1 + 1
MsgBox "Counter1 = " & counter1, , X
End If

If X > 1.2 And X <= 12 Then
counter2 = counter2 + 1
MsgBox "Counter2 = " & counter2, , X
End If
End Sub

Sub ResetXTest()
counter1 = 0
counter2 = 0
End Sub