Excel VBA instr如果语句错误

我试图寻找价值2,但是“不应该发生”显示,而不是其他的“好”。

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

我们知道价值在string内。 但由于某种原因,“不”不起作用。 有谁知道为什么?

那是因为

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

 ?not 4 -5 // bitwise not of 4 

这是一个值( cbool(-5) = true ),所以你需要:

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