Excel VBA instr if 语句为假
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17595164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Excel VBA instr if statement false
提问by user2569803
I'm trying to notlook for value 2, however "shouldn't happen" gets shown rather then the else, "ok".
我试图不寻找值 2,但是“不应该发生”被显示而不是其他,“好的”。
If Not InStr("1, 2, 3", "2") Then
MsgBox ("shouldn't happen")
Else
MsgBox ("ok")
End If
We know the value is within the string. yet for some reason the "not" is not working. Does anyone know why?
我们知道该值在字符串内。但由于某种原因,“不”不起作用。有谁知道为什么?
回答by Alex K.
Thats because
那是因为
?InStr("1, 2, 3", "2")
4
and
和
?not 4
-5 // bitwise not of 4
which is a truthyvalue (cbool(-5) = true
), so instead you need to:
这是一个真值 ( cbool(-5) = true
),因此您需要:
if InStr("1, 2, 3", "2") = 0 then
// not found
else
// found