Select … Case Statement

When a group of statements is executed, depending upon the value of an Expression, then Switch Case is used. 

If you have several conditions to check, then the If condition will go through each of the conditions separately. Hence, the job becomes a bit tedious. The alternative of IF is jumping off to the statement that applies to the state of a condition is Select Case. Each value is called a Case. Using the Select Case condition, you can choose from two or more options.

The Select CASE statement executes the code for the first condition that is found to be TRUE. If none of the conditions are met, then the Else clause in the CASE statement will be implemented. The Else clause is optional. If the Else clause is omitted and no condition is found to be true, then the CASE statement will do nothing.

Syntax

Example 1: Let’s suppose we have a country name in cell A2, which can be either UK, US, or India, and depending on which country we have, we will place the value of its capital in cell B2. The program will look like following

Output

Select Case Statement VBA

Example 2: By using the Select Case condition, write a macro, and implement the following condition

  1. If the cell value is less than 100 return North.
  2. If it’s less than 200, it should return South,
  3. For value less than 300, return East
  4. Else it should return West

Output

Select Case Statement2

Example 3: Write a macro to check the divisibility of numbers using Select Case conditions.

Output

Select … Case Statement

Pin It on Pinterest

Share This