×

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

VBA Pivot Table Grouping

VBA- Pivot Table Grouping For an instance, if in our pivot table, we have 11 different age groups from 20 to 30 -  but there might be a possibility that we...

2 minutes read.

Excel VBA: IF…..THEN …ELSE Statement

VBA : IF…..THEN …ELSE Statement: This function enables you to check one condition and, based on that, then run one of the two statement blocks present. If the ‘IF’ condition...

2 minutes read.

Excel VBA InputBox

Input Box The InputBox function in VBA is used to prompt the users to enter values. The user can click either the OK button or can choose the CANCEL button. If the user clicks...

3 minutes read.

Excel VBA Sgn Function

VBA Sgn Function: The Sgn function in VBA returns an integer (+1, 0, or -1), stating the arithmetic sign for the specified number. Syntax Sgn (Number) Parameter Number (required) –This parameter represents the number that...

1 minute 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 Left Function

VBA Left Function: The Left function in VBA returns a substring from the start of the specified string. Syntax Left (Str, Length) Parameter Str (required) – This parameter represents the string that you want to extract...

1 minute read.

Declaring a Variable in VBA

Declaring a Variable A variable is broadly described as a storage location combined with a name and representing a specific value. Declaring a variable is instructing the computer to reserve space...

2 minutes read.

Excel VBA Log Function

VBA Log Function: The Log function in VBA returns the natural logarithm for the specified number. Syntax Log (Number) Parameter Number (required) –This parameter represents a positive numeric value that you want to find the...

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.

How to save Macro Workbook?

Saving Macro Workbook Default Excel File Extension The default excel file extension is “.xlsx”. But the standard file extension “.xlsx” cannot contains macros. So, the workbook contains macro when save din.xlsx file, all VBA...

1 minute 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 Minute Function

The Minute function in VBA returns the minute component for the specified time. Syntax Minute (Time) Parameter Time (required) – This parameter represents the time. Return This function returns the minute component for the specified time. Example 1 Sub MinteFunction_Example1() ...

1 minute read.

VBA Not Equal Operator

What is VBA Not Equal Operator? VBA Not Equal binary operator (“<>”) is a logical function that is used to check if the specified values are not equal or not. This...

5 minutes read.

Excel VBA Now Function

Excel VBA Now Function: The Now function in VBA returns the current date and time.  Syntax Now () Parameter NA Return This function returns the current date and time.  Example 1 Sub NowFunction_Example1() 'It will return...

1 minute read.

Excel VBA Val Function

VBA Val Function: The Val function in VBA converts the given string into a numeric value. This function ignores spaces and continues to read the characters after space(s). It stops...

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.

Excel VBA Atn Function

VBA Atn Function: The Atn function in VBA returns the arctangent between quadrant -?/2 and +?/2 for the specified number, in radians. Syntax Atn (Number) Parameter Number (required) – This parameter represents the number that...

1 minute read.

VBA Type Mismatch Error

What is a Type Mismatch Error? VBA Type Mismatch Error is a run time error in excel, which often occurs when the data types contained in a VBA code are not matched...

3 minutes 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.

Excel VBA CSng Function

VBA CSng Function: The CSng function in VBA converts the supplied expression into a single-precision floating-point number (single data type). Syntax CSng (Expression) Parameter Expression (required) – This parameter represents the expression that you want...

1 minute read.