All of the logical operators in VBA can be thought of as "overrides" of the bitwise operators of the same name. Technically, they are always treated as bitwise operators. All of the comparison operators in VBA return a Boolean, which will always have none of its bits set (False
) or all of its bits set (True
). But it will treat a value with any bit set as True
. This means that the result of the casting the bitwise result of an expression to a Boolean
(see Comparison Operators) will always be the same as treating it as a logical expression.
Assigning the result of an expression using one of these operators will give the bitwise result. Note that in the truth tables below, 0
is equivalent to False
and 1
is equivalent to True
.
And
Returns True
if the expressions on both sides evaluate to True
.
Left-hand Operand | Right-hand Operand | Result |
---|---|---|
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
Or
Returns True
if either side of the expression evaluates to True
.
Left-hand Operand | Right-hand Operand | Result |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 1 |
Not
Returns True
if the expression evaluates to False
and False
if the expression evaluations to True
.
Right-hand Operand | Result |
---|---|
0 | 1 |
1 | 0 |
Not
is the only operand without a Left-hand operand. The Visual Basic Editor will automatically simplify expressions with a left hand argument. If you type...
Debug.Print x Not y
...the VBE will change the line to:
Debug.Print Not x
Similar simplifications will be made to any expression that contains a left-hand operand (including expressions) for Not
.
Xor
Also known as "exclusive or". Returns True
if both expressions evaluate to different results.
Left-hand Operand | Right-hand Operand | Result |
---|---|---|
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
Note that although the Xor
operator can be used like a logical operator, there is absolutely no reason to do so as it gives the same result as the comparison operator <>
.
Eqv
Also known as "equivalence". Returns True
when both expressions evaluate to the same result.
Left-hand Operand | Right-hand Operand | Result |
---|---|---|
0 | 0 | 1 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
Note that the Eqv
function is very rarely used as x Eqv y
is equivalent to the much more readable Not (x Xor y)
.
Imp
Also known as "implication". Returns True
if both operands are the same or the second operand is True
.
Left-hand Operand | Right-hand Operand | Result |
---|---|---|
0 | 0 | 1 |
0 | 1 | 1 |
1 | 0 | 0 |
1 | 1 | 1 |
Note that the Imp
function is very rarely used. A good rule of thumb is that if you can't explain what it means, you should use another construct.