11.2. Testing Multiple Conditions by Using Logical Operators

Often, you'll need to test two or more conditions before taking an action: if statement X is True and statement Y is True, then do this; if statement X is True or statement Y is True, then do the other; if statement X is True and statement Y isn't True, then find something else to do; and so on. For example, if it's raining and you have a cold, put on your warmest rain gear.

To test multiple conditions, you use VBA's logical operators to link the conditions together. Table 11.2 lists the logical operators that VBA supports, with short examples and comments.

Table 11.2. VBA's Logical Operators
OperatorMeaningExampleComments
AndConjunctionIf ActiveWorkbook.FullName = "c:\temp\Example.xlsm" ...

Get Mastering VBA for Microsoft Office 2007, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.