Share this Page URL

A VBA Language Primer > Functions and Subprocedures - Pg. 418

Note that the If Then portion of the code is paired with an End If statement to block off the conditional code. Test multiple conditions using the And and/or Or keywords. For multiple conditions involving both And and Or operations, you may need to group conditions together with parentheses so that the compound condition is evaluated correctly. For exam- ple, the following test for a suitable car will be true if the car is a red V6, or if it's just a sports car (Example 18-15). Example 18-15. Using And and OR operations strEngine = "V6" and strColor = "red" or strStyle = "sports car" The And operator has precedence over the Or operator. This means that And opera- tions are evaluated first, just as in math, where multiplication has precedence over addition. In our example, this groups the "red" and "V6" conditions. We could force a different precedence and look for a car that's a V6 and is either red or sporty by adding parentheses (again like in math), which explicitly group conditions (Example 18-16). Example 18-16. Grouping Conditions strEngine = "V6" and (strColor = "red" or strStyle = "sports car") Functions and Subprocedures