Subscriber only lesson.
Sign up to this course to view this lesson.
About this lesson
Understanding how the Select Case construct adds another logic test to your coding arsenal.
Download this lesson’s exercise file.Logic Tests: Select Case.xlsm
Logic Tests: Select Case
A logic structure to avoid nesting multiple If statements
When to use
Useful when you need to test something that could have multiple potential outcomes.
Structure of the Select Case framework:
Select Case <property>
Case Is = <result1>, <result2>
‘Do something if result1 or result2 are met
Case Is = <result3>
‘Do something different if result3 is met
‘Do something else since no previous test was true
We can perform a variety of tests on the Case Is line as follows:
|Test to perform||Character to use|
|Less than or equal to||<=|
|Greater than or equal to||>=|
|Not equal to||<>|
Hints & tips
- You can include as many Case Is lines in your code as you need
- It is optional (but recommended) to include a Case Else line to catch any exceptions
- Every case statement must end with End Select
- You can nest more Select Case (or If Then) blocks inside a case statement for more complex scenarios
Lesson notes are only available for subscribers.