Locked lesson.
About this lesson
Understanding how the Select Case construct adds another logic test to your coding arsenal.
Quick reference
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.
Instructions
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
Case Else
‘Do something else since no previous test was true
End Select
Testing Operators
We can perform a variety of tests on the Case Is line as follows:
Test to perform | Character to use |
Equal to | = |
Less than | < |
Greater than | > |
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.