O'Reilly logo

VB & VBA in a Nutshell: The Language by Paul Lomax

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

C.4. Logical and Bitwise Operators

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 both expression1 and expression2 evaluate to 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 of x is five and the value of y is less than seven.

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 expression1 or expression2 evaluates to True, or if both expression1 and expression2 evaluate to True, the result is True. Only if neither expression is True does the Or operation return False. If either ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required