在我的情况下,使用 OR 不起作用,我必须使用 AND,我不知道为什么。
Sub blah()
If LCase(Environ("username")) <> "me" Or LCase(Environ("username")) <> "some other user" Then '''DOESN'T WORK
Stop
ElseIf LCase(Environ("username")) <> "me" And LCase(Environ("username")) <> "some other user" Then '''WORKS
Stop
End If
End Sub
最佳答案
您的示例很难理解,但您可能只是对运算符的性质感到困惑。
想想真值表; Or
的结果操作是True
如果至少有一个操作数是 True
:
True Or True => True
True Or False => True
False Or True => True
False Or False => False
同样,
And
的结果操作是True
如果两个操作数都是 True
:True And True => True
True And False => False
False And True => False
False And False => False
如果有帮助,您可以可视化
Or
作为 +
(加法)和And
作为 *
(乘法)操作,使用 False
正在 0
和 True
“不为零”:1 Or 1 => 1 + 1 => True
1 Or 0 => 1 + 0 => True
0 Or 1 => 0 + 1 => True
0 Or 0 => 0 + 0 => False
1 And 1 => 1 * 1 => True
1 And 0 => 1 * 0 => False
0 And 1 => 0 * 1 => False
0 And 0 => 0 * 0 => False
关于excel - 为什么 'AND' 在这里不工作,而 'OR' 在这里工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56066576/