×

Excel VBA: Select … Case Statement

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

Select Case <Expression>
    Case <Expression1>
                 <Statement1>
    Case <Expression2>
                 <Statement2>
 …..
    Case <ExpressionN>
              <StatementN>
 [ Case Else 
       result_else ]
 End Select  

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

Sub Select_Case_Example1()
     Select Case Worksheets("Sheet1").Cells(2, 1).Value
         'Will check if the cell A2 value is Uk
         'If returns true, it will return London in B2
         Case Is = "UK"
         Worksheets("Sheet1").Cells(2, 2).Value = "London"
         'Will check if the cell A2 value is US
         'If returns true, it will return Washington D.C in B2
         Case Is = "US" 
         Worksheets("Sheet1").Cells(2, 2).Value = "Washington D.C"
         'Will check if the cell A2 value is India
         'If returns true, it will return New Delhi in B2
         Case Is = "India"
         Worksheets("Sheet1").Cells(2, 2).Value = "New Delhi"
     End Select
 End Sub 

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
Sub Select_Case_Example2()
     Select Case Worksheets("Sheet1").Cells(2, 1).Value
         Case Is < 100
             Worksheets("Sheet1").Cells(2, 2).Value = "North"
         Case Is < 200
             Worksheets("Sheet1").Cells(2, 2).Value = "South"
         Case Is < 300
             Worksheets("Sheet1").Cells(2, 2).Value = "East"
         Case Else 
             Worksheets("Sheet1").Cells(2, 2).Value = "West"
    End Select
 End Sub 

Output

Select Case Statement2

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

Sub Select_Case_Example3()
    Dim Var As Integer
    MyVar = 5
    Select Case MyVar
       Case 2
          MsgBox "The Number is a multiple of 2."
       Case 3
          MsgBox "The Number is a multiple of 3."
       Case 5 
          MsgBox "The Number is a multiple of 5."
       Case Else
          MsgBox "Unknown Number"
    End Select
 End Sub 

Output

Select … Case Statement

Related Topics

Excel VBA Conditional Statement

Conditional Statement in VBA Excel Conditional Statements in Excel VBA are one of the most powerful and useful features in programming, this will give you to perform comparisons to decide or...

2 minutes read.

Steps to Create a Pivot Table

Steps to Create a Pivot Table: Pivot Tables can be easily automated through VBA coding. To create a Pivot Table in VBA, follow the below step by step procedure to...

4 minutes read.

Excel VBA Day Function

The function Day in VBA returns the day number (from 1 to 31) for the given date value. Syntax Day (Date) Parameter Date (required) – This parameter represents the date. Return This function returns the day...

1 minute read.

Introduction to Visual Basic Editor Window

How to enable the Developer Ribbon Tab? In order to work with VBA, users need to make a small change in Excel to display a new tab (Developer) at the top of the...

5 minutes read.

VBA Global Variable

What is Global Variable? The Global Variables in VBA refers to the variables declared before the start of any macro. They are defined outside the functions and are used by all the functions or...

6 minutes read.

For Next loop in VBA

For Next Loop The ”For Next” loop is used for a fixed number of times. It works by implementing the loop for the specified number of times. In this, the user specifies how...

3 minutes read.

Excel VBA IsDate Function

VBA IsDate Function: The IsDate function in VBA returns a Boolean value indicating whether the given expression is interpreted as a VBA Date or not. Syntax IsDate (Expression) Parameter Expression (required) – This parameter...

1 minute read.

Excel VBA Trim Function

The Trim function in VBA removes the leading and trailing spaces from the specified string. Syntax Trim (String) Parameter String (required) – This parameter represents the string from which you want to remove the leading and...

1 minute read.

Excel VBA Filter Function

VBA Filter Function: The Filter function in VBA returns a subset for the given string array, based on specified criteria. Syntax Filter (SourceArray, Match, [Include], [Compare]) Parameter SourceArray (required) – This parameter the array of Strings that you...

2 minutes read.

VBA Cell

What is VBA cell? Cells is one of the elements (workbook, worksheet, range) in Excel VBA, which refers to cells of the Excel worksheet.  In VBA, the cell is also a property...

5 minutes read.

Excel VBA CBool Function

VBA CBool Function: The CBool function in VBA calculates an expression and returns the result as a Boolean data type. Syntax CBool (Expression) Parameter Expression (required) – This parameter represents the expression that that you want...

1 minute read.

VBA ActiveCell Property

What is the ActiveCell Property? The active cell signifies the active selected cell in the current worksheet. The Active property acts as a reference point and is used to move the cell cursor...

5 minutes read.

Excel VBA LTrim Function

VBA LTrim Function: The LTrim function in VBA removes the leading spaces from a supplied text string. Syntax LTrim (String) Parameter String (required) - This parameter represents he text string that you want to remove...

1 minute read.

Excel VBA Chr Function

VBA Chr Function: The Chr function in VBA returns the character equivalent to a supplied character code between 0 and 255. Syntax Chr (CharCode) Parameter CharCode (required) – This parameter represents the character code for...

1 minute read.

UserForm and its Properties Excel VBA

UserForm and its Properties Userform has certain properties that can be viewed as category wise (based on appearance, behavior, font) or in an alphabetic manner. The property window is used to set or...

11 minutes read.

Excel VBA Oct Function

VBA Oct Function: The Oct function in VBA converts the given number into octal notation and returns the result as a string. Syntax Oct (Number) Parameter Number (required) – This parameter represents the numeric value...

1 minute read.

Excel VBA UBound Function

Excel VBA UBound Function: The UBound function in VBA returns the highest subscript for the specified dimension in the given array. Syntax UBound (ArrayName, [Dimension]) Parameter ArrayName (required) – This parameter represents an array for which...

1 minute read.

Excel VBA DatePart Function

The DatePart function in VBA returns a part (day, month, week, etc.) for the specified date and/or time. Syntax DatePart (Interval, Date, [FirstDayOfWeek], [FirstWeekOfYear]) Parameter Interval (required) – This parameter represents a string specifying the interval to be used. It can...

2 minutes read.

Excel VBA IsNull Function

VBA IsNull Function: The IsNull function in VBA returns a Boolean, indicating whether a supplied expression is Null. Syntax IsNull (Expression) Parameter Expression (required)- This parameter represents the name of the argument that you want...

1 minute read.

Userform Events

What are Events? Anything you do in to trigger an excel file is an event (an action). Example: If you want a greeting message ‘Good Day’ whenever an excel file is...

7 minutes read.