Logical Operators in VBA

VBA logical operators - symbols that instruct the compiler to perform logical operations like conjunction and disjunction - are explored herein.

In this article:

9 minutes read

VBA Operators

VBA operators are special tokens that instruct the compiler to “operate” on values or program identifiers. They are also referred to as program symbols or code elements. Also, the program identifiers they operate on (e.g., addition) must be capable of holding values.

You may have heard of the term operand. These are the values (i.e., literals) or identifiers (e.g., variables and constants) that operators act on. Based on their number of operands, operators are either unary or binary.

Unary VBA operators are operators that only act on a single operand at a time. Binary VBA operators are those that only act on two operands at a time.

VBA supports the following categories of operators:

This article takes an in-depth look at the VBA logical operators.

VBA Operator Precedence

Often, an operation involves more than one operator, sometimes of different categories. So, it is worthwhile paying special attention to the order of precedence of the VBA operators. Otherwise, VBA’s default operator precedence applies.

The default precedence is as follows: arithmetic first, then concatenation, followed by comparison, then logical, and finally assignment operators.

Moreover, there is a pre-set precedence for operators within each category. How do you override this default order and remove any ambiguity? Easy, set the preferred precedence with open and close parentheses, ( ).

Logical Operators

These operators perform logical negation, conjunction, disjunction, exclusion, equivalence, and implication operations. They do so on either one operand (unary operation) or a pair of operands (binary operation).

The table below summarizes the various such operators supported by VBA.

OperatorDescriptionReturn Data TypeOrder
NotLogical Negation (unary) operator returns the reverse logical result of an expression.BooleanTrue (i.e., ≠ 0) if the expression is False (i.e., = 0);

BooleanFalse if the expression is True;

Null if the expression is Null.
1
AndLogical Conjunction (binary) operator performs a logical conjunction on two expressions.BooleanTrue if both expressions are True;

BooleanFalse if either expression is False;

Null if either expression is Null.
2
OrLogical Disjunction (binary) operator performs a logical disjunction on two expressions.BooleanTrue if either expression is True;

BooleanFalse if both expressions are False;

Null if both expressions are Null, or one expression is Null and the other is False.
3
XorLogical Exclusion (binary) operator performs a logical exclusion on two expressions.BooleanTrue if one, and only one, expression is True;

BooleanFalse if both expressions are True or False;

Null if either expression is Null.
4
EqvLogical Equivalence (binary) operator performs a logical equivalence on two expressions.BooleanTrue if both expressions are True or False;

BooleanFalse if one expression is True and the other is False, and vice versa;

Null if either expression is Null.
5
ImpLogical Implication (binary) operator performs a logical implication on two expressions.BooleanTrue if:
  • Both expressions are True or False;
  • First expression is False, and the second expression is True or Null;
  • First expression is Null, and the second expression is True.

BooleanFalse if, and only if, the first expression is True, and the second expression is False;

Null if either expression is Null.
6

As shown in the table, these operators all have different precedence. So, when several of them appear in an expression, evaluation proceeds in the order of appearance from left to right.

Also, enclosing a part of an expression in parentheses causes it to be evaluated ahead of other parts.

Logical Operators: Negation (Not)

The sample code below demonstrates the logical negation (Not) operator’s usage.

Sample code illustrating the Not operator’s usage.
Sample code illustrating the Not logical operator’s usage.

Logical Operators: Conjunction (And)

The sample code below illustrates the logical conjunction (And) operator’s usage.

Sample code illustrating the And Logical Operator’s usage.
Sample code illustrating the And logical operator’s usage.

Logical Operators: Disjunction (Or)

The sample code below shows the logical disjunction (Or) operator’s usage.

Sample code illustrating the Or Logical Operator’s usage.
Sample code illustrating the Or logical operator’s usage.

Logical Operators: Exclusion (Xor)

The sample code below illustrates the logical exclusion (Xor) operator’s usage.

Sample code illustrating the Xor operator’s usage.
Sample code illustrating the Xor logical operator’s usage.

Logical Operators: Equivalence (Eqv)

The sample code below demonstrates the logical exclusion (Eqv) operator’s usage.

Sample code illustrating the Eqv Logical Operator’s usage.
Sample code illustrating the Eqv logical operator’s usage.

Logical Operator: Implication (Imp)

The sample code below illustrates the logical exclusion (Imp) operator’s usage.

Sample code illustrating the Imp Logical Operator’s usage.
Sample code illustrating the Imp logical operator’s usage.
5 1 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Keep Learning

Related Articles

Keep Learning

Related Articles