I recently learned that Excel's Find function returns a #VALUE error when it doesn't find the needle in the haystack (i.e. no match is found). I have several questions about this behavior:
The Find function looks for a case-sensitive match, and can be tested with IsNumber, like:
=IF(ISNUMBER(FIND("abc",A1)),FIND("abc",A1),"No exact match")
There is a very similar function called Search which does the same thing, but is not case sensitive. It also returns an error if no match is found.
So if you are looking for something and want to return -1 if there is no match and you are not worried about being case sensitive, then something like this should work for you:
=IF(COUNTIF(A1,"*abc*")=0,-1,SEARCH("abc",A1))
For case sensitive searches, it would be:
=IF(ISNUMBER(FIND("abc",A1)),FIND("abc",A1),-1)
If you are on Excel 2007 or later, you can skip the error checking by using IFERROR:
=IFERROR(SEARCH("abc",A1),-1)
=IFERROR(FIND("abc",A1),-1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With