How to Use Select Case Statement in Excel VBA (2 Examples)

Download Practice Workbook


Using the sample dataset below, we will illustrate two examples to use the Select Case statement in Excel VBA. This statement is an alternative to the IF-ELSE statement.

Dataset for excel vba select case


Example 1 – Apply CommandButton to Use Select Case

Steps:

  • Go to the Developer tab.
  • Select Insert.
  • Choose the command button from ActiveX Controls.

  • Draw a CommandButton.
  • Right-click your mouse.
  • In the Context menu, select view code.

CommandButton to use excel vba select case

  • A new VBA module will open. Enter the following code in that module.
Private Sub CommandButton1_Click()
Dim Mark As Integer, Grade As String
Mark = Range("C11").Value
Select Case Mark
    Case Is >= 90
        Grade = "A"
    Case Is >= 80
        Grade = "B"
    Case Is >= 70
        Grade = "C"
    Case Is >= 60
        Grade = "D"
    Case Else
        Grade = "F"
End Select
Range("C12").Value = Grade
End Sub

Code Explanation:

  • We have created two variables Mark and Grade using the dim statement.
  • The Mark variable is Integer and the Grade is String.
  • We entered the marks in C11.
  • We have set Mark as our Case and applied the conditions.
  • Go back to the dataset.
  • Enter a mark. (For example, 94)
  • Select the command button.

CommandButton to use excel vba select case

  • Excel will determine the grade.

Read More: How to Open Workbook from Path Using Excel VBA (4 Examples)


Similar Readings


Example 2 – Insert InputBox and MsgBox to Use Select Case

Steps:

  • Go to the Developer tab.
  • Select Visual Basic.

MsgBox and InputBox to use excel vba select case

  • The Visual Basic window will open.
  • Go to Insert.
  • Select Module.

  • Excel will create a new module. Enter the following code into the module.
Sub Select_Case_Statement()
        Dim Mark  As Integer
        Dim Grade As String
    Mark = InputBox("Enter Mark")
    Select Case Mark
        Case Is >= 90
            Grade = "A"
        Case Is >= 80
            Grade = "B"
        Case Is >= 70
            Grade = "C"
        Case Is >= 60
             Grade = "D"
        Case Else
            Grade = "F"
   End Select
    MsgBox "The Student got : " & Grade
End Sub

MsgBox and InputBox to use excel vba select case

  • Press F5 to run the code.
  • Excel will show the following input box.
  • Enter any number.
  • Click OK.

  • Excel will show the grade in a message box. 

MsgBox and InputBox to use excel vba select case

Read More: Learn Excel VBA Programming & Macros (Free Tutorial – Step by Step)


Things to Remember

  • The Select Case statement is an alternative to the IF-ELSE
  • You can press ALT+F11 to get the Visual Basic Window.
  • Activate Design Mode to edit the command button.

Related Articles

Get FREE Advanced Excel Exercises with Solutions!

Tags:

Taryn Nefdt
Taryn Nefdt

Taryn is a Microsoft Certified Professional, who has used Office Applications such as Excel and Access extensively, in her interdisciplinary academic career and work experience. She has a background in biochemistry, Geographical Information Systems (GIS), and biofuels. She enjoys showcasing the functionality of Excel in various disciplines. She has over ten years of experience using Excel and Access to create advanced integrated solutions.

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo