Conditionally Execute A Group Of Statements

You can conditionally execute a group of statements by using the If Then conditional statement. The If Then conditional statement checks to see if a specific condition is true and, if so, executes all statements between the Then keyword and the End If statement. For example, you can use the If Then statement to make sure variables contain a numeric value before summing.

The If Then conditional statement always consists of three different parts. The If keyword always initiates the If Then conditional statement. A condition always follows, such as A = 5, followed by the keyword Then. The body contains the statements to execute if the specified condition is true. Finally, the End If statement indicates the end of the conditional statement.

condition is false, your macro ignores the statements. The If Then statement also enables you to specify a group of statements to execute only if the condition is false, by using the Else statement.

You can nest If Then statements together to check multiple conditions. Nesting the statements together means that you place one If Then statement inside of another. For example, you can check the value of a cell to make sure it is numeric and that it contains a number greater than 0. You can accomplish this with nested If Then statements.

With nested If Then statements, the first If Then statement must be True, or your macro never sees the condition specified by the second If Then statement.

The statements you specify between the If Then and End If statements only execute if the condition is true. If the

CONDITIONALLY EXECUTE A GROUP OF STATEMENTS

CONDITIONALLY EXECUTE A GROUP OF STATEMENTS

'—H Create a new subrouti

—0 Type If Condition Then, replacing Condition with the statement to check.

-0 Type the statements to execute if condition is true.

^0 Type the statements to execute if the condition is false.

'—H Create a new subrouti

—0 Type If Condition Then, replacing Condition with the statement to check.

-0 Type the statements to execute if condition is true.

^0 Type the statements to execute if the condition is false.

0 0

Post a comment