Select Case
- 02:11
How Select Case can be used to develop more sophisticated optionality in a procedure.
Downloads
No associated resources to download.
Transcript
Select case offers a range of options if certain criteria are met.
This is possible with if then else coding, but select case provides a neater and more efficient solution.
Select case is typically used to assess a variable and a set of different instructions depending on whether the variable meets certain criteria.
In this example, depending on the value entered into the input box, a variable named charge is assigned a certain value.
Let's take a look at the select case syntax.
The select case instruction begins with select case and an expression to assess is stated.
The expression could refer to a variable or a formula.
In this example, the expression being assessed is cost, and we can see in the line above that cost takes the value of the number entered into a VBA input box.
The next line requires the word case, and we state the condition we want to assess our expression against.
In this example, the condition is speech mark. Speech mark ie, No value has been entered into the input box.
The next line states the instructions to carry out if this condition is met, and if the condition is met where no value is entered into the input box.
Here we exit the sub procedure.
Select case can be used to provide the instructions if a case condition is met, and if a case condition is not met here, we would use case else rather like an if and then statement.
However, it's more effective when we have a number of case conditions where we require different instructions.