Search code examples
excelstringintegerexcel-2016boolean-operations

Excel: string vs integer comparison equals TRUE, but why?


In my Excel file I have some data that contains either a dash - or an integer. The data looks as follows:

enter image description here

Following, I wish to check whether these values are higher than some particular reference integer. I use the following (cleaned) INDEX statement to get the individual values:

INDEX($E6:$DL6;<Row>;<Column>)

which I then compare to the reference value (located in DO$4 in this case), and return 1 if True, 0 if False, and with an error a dash is again printed.

=IFERROR(IF(<Indexed value>=DO$4;1;0);"-")

I would assume that the comparison "-">=DO$4 would throw that error. However, for some reason, the if statement produces a 1, meaning that the comparison is True.

How is it possible that such a comparison does not produces an error? The function written out is as follows:

=IFERROR(IF(INDEX($E6:$DL6;1;<Column-indexer>)>=DO$4;1;0);"-")


Solution

  • A comparison of a string with a number doesn't product an error - the string (in this case a dash with an apostrophe in front of it) is always bigger.

    enter image description here

    You could put a VALUE function round the index so it would throw an error if it tried to convert a string

    =IFERROR(IF(VALUE(INDEX($E6:$DL6;1;<Column-indexer>))>=DO$4;1;0);"-")
    

    Or alternatively use ISNUMBER

    =IF(ISNUMBER(INDEX($E6:$DL6;1;<Column-indexer>)),if(INDEX($E6:$DL6;1;<Column-indexer>)>=DO$4;1;0);"-")
    

    There is a slight difference - if the first formula encounters a number formatted as text, it will convert it to a number, but the second one will treat it as text.