Simple comparison in VBA is evaluating incorrectly. RRS feed

  • Question

  • I have a simple condition based on the cell's value.

    The strange thing is that even when the cell's value is exactly =1 it goes into the wrong condition .

           If Target.Value >= 0 Then
                If Target.Value > 1 Then 'since all the cells have % format
                    Call showError(Target)
                    isValueAllowed = True
                    Exit Function
                End If
                Call showError(Target, errorMsgZero)
            End If

    Even if the Target.Value is 1 , it's calling showError function. The format of the cell is set as %.

    I have tried printing the value and it's 1. I have several other conditions like this and each one is evaluating opposite of what is actually expected.

    Could anyone please guide what is issue here.


    Friday, July 19, 2019 7:15 AM

All replies

  • If the value is a string containing “1%”, instead of convenient numbers, then maybe try this:

       If Val(Target.Value) > 1 Then . . .

    • Edited by Viorel_MVP Friday, July 19, 2019 8:01 AM
    Friday, July 19, 2019 8:01 AM
  • I made workaround and used  If cstr(Target.Value)>1 , do you think it's fool proof, else I will modify as per your suggestion.


    Friday, July 19, 2019 8:57 AM