Consulting

Results 1 to 4 of 4

Thread: Strange VBA Operation Outcome

  1. #1

    Strange VBA Operation Outcome

    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?
    Attached Images Attached Images

  2. #2
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    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.

  3. #3
    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

  4. #4
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    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.
    [VBA]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[/VBA]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •