Excel VBA instr if statement false

I try not to look for the value 2, however, "should not be" is displayed, not else, "ok".

If Not InStr("1, 2, 3", "2") Then MsgBox ("shouldn't happen") Else MsgBox ("ok") End If 

We know that the value is inside the string. but for some reason, "no" does not work. Does anyone know why?

+4
source share
1 answer

It's because

 ?InStr("1, 2, 3", "2") 4 

and

 ?not 4 -5 // bitwise not of 4 

which is the true value ( cbool(-5) = true ), so you need to:

 if InStr("1, 2, 3", "2") = 0 then // not found else // found 
+6
source

Source: https://habr.com/ru/post/1490911/


All Articles