## Logical Operators with VBA

Logic as applied to a computer program is evaluating an expression as true or false. An expression is typically, but not always, a comparison of two variables such as var1>var2 or var1=var2 (see Table 3.1 for a list of available comparison operators). A programmer reads these expressions as follows:

• The value of var1 is greater than the value of var2.

• The value of var1 equals the value of var2.

### The statements are evaluated as true or false.

Imagine a simple device that takes a single expression as input, evaluates that expression as true or false, spits out the answer, and then moves on to the next expression. The evaluation of the expression is a simple task since there are only two choices and computers are very good at assigning 1's (true) or 0's (false) to things. The difficulty arises from trying to make sense out of the expressions that have been evaluated as true or false. This is where Boolean (after the nineteenth century mathematician George Boole) algebra comes in to play. Boolean algebra refers to the use of the operators AND, OR, NOT, and a few others to evaluate one or more expressions as true or false. Then, based on the result of the logic, the program selects a direction in which to proceed.

Table 3.1 Comparison Operators in VBA

Operator Function

= Tests for equality

<> Tests for inequality

< Less than

> Greater than

<= Less than or equal to

>= Greater than or equal to

AND, OR, and NOT Operators

VBA uses logical AND to make a decision based on the value of two conditions. The value of each condition can be one of two values, true or false. Consider the following two conditions.

Condition 1 Condition2

The expression Condition1 AND Condition2 evaluates as true only if Condition1 and Condition2 are both true. If either or both conditions evaluate to false then the overall result is false. The evaluation of expressions using logical operators is easily displayed in truth tables. Table 3.2 shows the truth table for logical AND.

Table 3.2 Truth Table for the AND Operator

Conditionl Condition2 Conditionl AND Condition2

True True True

True False False

False True False

False False False

The logical operator OR returns true from an expression when at least one of the conditions within the expression is true.

The expression Condition1 OR Condition2 evaluates as true when either Condition1 or Condition2 is true or if both conditions are true. Table 3.3 shows the truth table for logical OR.

Table 3.3 Truth Table for the OR Operator

Conditionl Condition2 Conditionl OR Condition2

True True True

True False True

False True True

False False False

The NOT operator simply returns the opposite logic of the condition; so if the condition is false, NOT will return true and vice versa. Table 3.4 shows the truth table.

There are a few other logical operators (Xor, Eqv, and Imp) but they are seldom used or needed, so let's turn our attention to the practical use of Boolean algebra within the code structures If/Then/Else and Select Case.

Table 3.4 Truth Table for the NOT Operator

Condition1 NOT Condition!

True False

False True ## Biorhythm Awareness

Who else wants to take advantage of biorhythm awareness to avoid premature death, escape life threatening diseases, eliminate most of your life altering mistakes and banish catastrophic events from your life.

Get My Free Ebook