

In Excel, open the VBA Editor by pressing F-11 (or press the Visual Basic button on the Developer ribbon.) Once we have the logic correct, we will apply the logic to a range of cells using a looping structure.

In this example we will evaluate a single cell. Similarly, you can ask several questions and if any single or multiple of questions are true, the action will be performed. Like the AND function, you can ask several questions and all the questions must evaluate to TRUE to perform the action. The IF…THEN can also evaluate many conditions. Another test could be to test the value of a cell, such as “Is the cell value greater than 100?” If so, display the message “Great sale!” Otherwise, display the message “Better luck next time.” You give the IF a condition to test, such as “Is the customer a “preferred” customer?” If the customer is classified as “preferred” then calculate a discount amount. The IF…THEN statement is like the IF function in Excel. The IF…THEN statement allows you to build logical thinking inside your macro. The IF…THEN statement is one of the most commonly used and most useful statements in VBA.
