Excel VBA Logical Operators

In this section you will be able to learn the basic logical operators used in most of the programming language. If your familiar with Digital logic or switch, then this is the best comparison.

In digital logic, it uses binary system 1 and 0.
In switches, it uses open or close. To understand the logical error, we can use switch as an example.
Series Connection

Parallel Connection

For series connection, it is the same as AND operator. Why?
Let's discuss first the AND operator.

AND
   - if both switches is close, then the lamp will light.
   - if one of the switches is open, then the lamp will not light.
   - if both switches is open, then the lamp will not light.
 
Let's assume that close is equal to 1 and open equal to 0.
We can make actually a logical table for AND.

AND Logical table

When we use it in VBA,
    1 = TRUE
    0 = FALSE

For parallel connection, it is the same as OR operator. Why?
Let's discuss first the OR operator.

OR
   - if both switches is close, then the lamp will light.
   - if one of the switches is open, then the lamp will still light.
   - if both switches is open, then the lamp will not light.
 
Let's assume that close is equal to 1 and open equal to 0.
We can make actually a logical table for OR.

OR Logical Table

I hope you have now clear idea on when to use OR/AND operator.

Just always remember, when using AND, you can have a TRUE answer if both input are TRUE,
and when using OR, you can have a FALSE answer if both input are FALSE.

    




No comments:

Post a Comment