Execute A Statement Based Upon The Value

You can create code to execute a specific block of code based upon the value of a statement using the Select Case statement. The Select Case statement is similar to specifying multiple If Then statements to check the value of the same expression. The Select Case statement works well if you have different conditions to execute based upon the value of a variable, such as determining in which state a user lives in order to calculate sales tax. For example, if you want to execute a different statement depending upon which value a user selects, you can either use multiple If Then statements to check the value of the response, or you can use one Select Case statement.

The Select Case statement is best suited for situations where you need to check one expression for several different values. The Select Case statement consists of four different parts. The Select Case statement includes the expression you want to check. For example, if you determine the value of the UserVal variable, your initial statement is Select Case UserVal. Each Case statement indicates a value for the expression. For example, Case 4 determines if UserVal = 4 is True. Under each Case statement are statements to execute if the expression equals the specified value. Finally, the End Select statement specifies the end of the Select Case statement.

When the Select Case statement finds a match to the value of the expression, the corresponding statements execute and exits the Select Case statement. You can also add a Case Else statement that tells the statement to run if none of the other Case statements are valid. For example, you can add a Case Else statement that executes if the value of UserVal is not one of the Case statement values.

EXECUTE A SPECIFIC STATEMENT BASED UPON THE VALUE

EXECUTE A SPECIFIC STATEMENT BASED UPON THE VALUE

—H Create a new subroutine.

■ You can type a loop to pass values to subroutine.

-Q Type Select Case Expression, replacing Expression with the expression to check for values.

—H Create a new subroutine.

—0 Declare any variables needed for the subroutine.

'—0 Initialize variables.

■ You can type a loop to pass values to subroutine.

loops through values in column B as long as they are not empty.

-Q Type Select Case Expression, replacing Expression with the expression to check for values.

_0 Type Case Value1, replacing Valuel with the first value to check for the specified expression.

'—0 Type the code statements to execute if expression equals specified value.

0 0

Post a comment