## Comparison operators

Computers do more than calculate - among other things they also make decisions. VBA macros can select a course of action that depends on the outcome of some decision. We have already encountered one way in which this can be done by using the If ... Then statements. This chapter will look at VBA decision-making statements in a little more detail.

When we used a statement like If cell.Value > 0 Then in previous chapters, the expression cell.Value has to be greater than 0 before the result will succeed. In other words, the result of the comparison must be true. This is called a comparison operator. Clearly, the comparison operators produce Boolean outcomes, because the outcome of the comparison is either true or false. The mathematical operators, discussed in previous chapters, produce numeric values, whereas the comparison operators produce Boolean results. Table 6.1 gives some illustrations of comparison operators.

Operator |
Example of use |
Description |

> |
This > that |
The greater than operator returns True if the value on the left side of > is numerically or alphabetically greater than the value on the right. |

< |
bonus < 2000.00 |
The less than operator returns True if the value on the left side of < is numerically or alphabetically less than the value on the right. |

= or Is |
Rng Is Nothing |
The Is operator (used to compare two object references) returns True if the object Rng is the same as the Nothing range. |

>= |
FirstName >= "Mike" |
The greater than or equal to operator returns True if the value on the left side of >= is numerically or alphabetically greater than or equal to the value on the right. |

<= |
Num <= cell.Value |
The less than or equal to operator returns True if the value on the left side of <= is numerically or alphabetically less than or equal to the value on the right. |

<> |
Range("D1") <> "Amy" |
The not equal to operator returns True if the value on the left side of <> is numerically or alphabetically unequal to the value on the right. |

## Post a comment