Select Case Statement

Select Case statement is similar to cascaded If Else statement. This is simpler than If else statement especially when have more than two conditions.

The syntax is like this:
 
     Select Case MyCase

           Case Is = Condition 1
                    ' Do something here
            Case Is = Condition 2
                      ' Do something here      
           Case Is = Condition 3
                      ' Do something here
           Case else
                   ' Do something here

      End Select

In the first case, it needs to pass the arguments if MyCase is equal to the condition 1.
Same with other cases.

Example:
Let's convert the example from previous lesson.



Try to read and understand the example code and write it in your VBA Editor.
This is much simpler than If else right?

Homework:
1. Create Select Case t inside a For Loop.
2. Create a sheet like this.

3. Loop these grade in range of cells from A1 to  A9
4. Your condition should be the following:
    Grade    Rating
    <70       Bad
    70-80    Fair
    81-90    Good
    91-100  Excellent

5. Rating should be inputted in column B.

Before we proceed to our next lesson, I will give you an examination to test your knowledge. Because, in the next tutorial it's already bit complicated so I need you to build firm foundation in VBA basic programming.

Are you ready to take the exam? Click here.



No comments:

Post a Comment