Using If...Then...Else Statements
You can use the If...Then...Else statement to run a specific statement or a block of statements, depending on the value of a condition. If...Then...Else statements can be nested to as many levels as you need. However, for readability, you may want to use a Select Case statement rather than multiple levels of nested If...Then...Else statements.
Running Statements if a Condition is True
To run only one statement when a condition is True, use the single-line syntax of the If...Then...Else statement. The following example shows the single-line syntax, omitting the Elsekeyword:
|
To run more than one line of code, you must use the multiple-line syntax. This syntax includes the End If statement, as shown in the following example:
|
Running Certain Statements if a Condition is True and Running Others if It's False
Use an If...Then...Else statement to define two blocks of executable statements: one block runs if the condition is True, the other block runs if the condition is False.
|
Testing a Second Condition if the First Condition is False
You can add ElseIf statements to an If...Then...Else statement to test a second condition if the first condition is False. For example, the following function procedure computes a bonus based on job classification. The statement following the Else statement runs if the conditions in all of the If and ElseIf statements are False.
|