Logical operators allow you to evaluate one or more expressions and return a logical value. VBA supports six logical operators: And, Or, Not, Eqv, Imp, and Xor. These operators also double as bitwise operators. A bitwise comparison examines each bit position in both expressions and sets or clears the corresponding bit in the result depending upon the operator used. The result of a bitwise operation is a numeric value.

*And*Performs logical conjunction; that is, it only returns

`True`if bothand*expression1*evaluate to*expression2*`True`. If either expression is`False`, then the result is`False`. If either expression is`Null`, then the result is`Null`. Its syntax is:result = expression1 And expression2

For example:

If x = 5 And y < 7 Then

In this case, the code after the

`If`statement is executed only if the value ofis five and the value of*x*is less than seven.*y*As a bitwise operator, And returns 1 if the compared bits in both expressions are 1, and returns in all other cases, as shown in the following table:

Bit in expression1 Bit in expression2 Result 0 0 0 0 1 0 1 0 0 1 1 1 For example, the result of 15 And 179 is 3, as the following binary representation shows:

00000011 = 00001111 And 10110011

*Or*Performs logical disjunction; that is, if either

or*expression1*evaluates to*expression2*`True`, or if bothand*expression1*evaluate to*expression2*`True`, the result is`True`. Only if neither expression is`True`does the`Or`operation return`False`. If either ...

Start Free Trial

No credit card required