EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 120+ Courses All in One Bundle
  • Login
Home VBA VBA Resources VBA Program Flow Functions VBA Switch Case
Secondary Sidebar
VBA Program Flow Functions
  • VBA Program Flow
    • VBA Switch
    • VBA IIF
    • VBA Switch Case

VBA Switch Case

By Madhuri ThakurMadhuri Thakur

VBA Switch Case

Excel VBA Switch Case

In VBA Switch Case, when we need to logically check or analyze a condition and execute the different lines of codes based on the same, we use IF-Else conditional statement. Similarly, there is one more statement Switch Case which is more widely known as Select Case statement which can check or analyze the multiple logical conditions and execute the code lines based on those. When you have three or more logical conditions to check, it is recommended that you use Select Case or Switch Case instead of a conventional IF-Else statement. Because it makes the VBA Code faster and more understandable instead of nested IF-Else statements in such cases.

All in One Financial Analyst Bundle(250+ Courses, 40+ Projects)
Financial ModelingInvestment BankingUS GAAPCFA-Level 1 & 2
Equity ResearchM & A ModelingPrivate Equity ModelingForex Trading
Price
View Courses
250+ Online Courses | 40+ Projects | 1000+ Hours | Verifiable Certificates | Lifetime Access
4.9 (86,704 ratings)

Switch Case / Select Case

Switch Case or Select Case in VBA works on similar lines as of IF-Else statements. It checks the multiple logical/conditional expressions against different values (cases). As soon as any of the case is matching the condition (becomes true for the given condition), the system executes that case and rest other cases are ignored to execute. In case, there are two cases that are true for the given condition, only the first one will be executed with the law of precedence.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax for Switch Case/Select Case is as below:

Select Case <Expression to check>
Case value_1
Code to Execute when Expression = value_1
Case value_2
Code to Execute when Expression = value_2
Case value_3
Code to Execute when Expression = value_3
.
.
.
Case value_n
Code to Execute when Expression = value_n
Case Else
Code to Execute when no case value meets the expression/logical test.
End Select

Where,

Expression: This is an expression for which we wanted to check or analyze using different case values.

value_1, value_2, value_3, … are the logical conditions that are needed to be checked for the given expression.

How to Use the Switch Case Statement in Excel VBA?

We will learn how to use the Switch Case statement in Excel by using the VBA Code.

You can download this VBA Switch Case Excel Template here – VBA Switch Case Excel Template

VBA Switch Case – Example #1

We will check whether the given number is less than 100 or more than 100. For this, follow the steps below:

Step 1: Insert a new module inside Visual Basic Editor (VBE). Click on Insert tab > select Module.

Insert Module

Step 2: Define a new subprocedure within the inserted module that can hold your macro.

Code:

Sub switch_case_example1()

End Sub

VBA Switch Case Example 1-1

Step 3: Define a new variable named usrInpt which can hold the user value. Make use of the VBA InputBox function to create an input box that takes user values through variable usrInpt.

Code:

Sub switch_case_example1()

Dim usrInpt As Integer
usrInpt = InputBox("Please enter your value")

End Sub

VBA InputBox function Example 1-2

Step 4: Use the Select Case statement and supply the value provided by the user through variable usrInpt. This is the expression we need to check with logical conditions.

Code:

Sub switch_case_example1()

Dim usrInpt As Integer
usrInpt = InputBox("Please enter your value")
Select Case usrInpt

End Sub

Use Select Case statement

Step 5: Enter the first logical test to be checked under the Case statement as follows.

Code:

Sub switch_case_example1()

Dim usrInpt As Integer
usrInpt = InputBox("Please enter your value")
Select Case usrInpt
Case Is < 100

End Sub

VBA Switch Case Example 1-4

Step 6: Use MsgBox function to add an output message if Case Is < 100 is true.

Code:

Sub switch_case_example1()

Dim usrInpt As Integer
usrInpt = InputBox("Please enter your value")
Select Case usrInpt
Case Is < 100
MsgBox "The provided number is less than 100"

End Sub

VBA Switch Case Example 1-5
Step 7: Now, we need to provide an executable statement when the value for usrInpt is greater than 100. Add a Case and MsgBox to achieve this.

Code:

Sub switch_case_example1()

Dim usrInpt As Integer
usrInpt = InputBox("Please enter your value")
Select Case usrInpt
Case Is < 100
MsgBox "The provided number is less than 100"
Case Is > 100
MsgBox "The provided number is greater than 100"

End Sub

VBA Switch Case Example 1-6

Step 8: What if the value provided by the user is exactly the 100? We have no case added for that. Let’s add the one that gives the user a message that the value he entered is 100.

Code:

Sub switch_case_example1()

Dim usrInpt As Integer
usrInpt = InputBox("Please enter your value")
Select Case usrInpt
Case Is < 100
MsgBox "The provided number is less than 100"
Case Is > 100
MsgBox "The provided number is greater than 100"
Case Is = 100
MsgBox "The provided number is 100"

End Sub

VBA Switch Case Example 1-7

Step 9: We need to end the Select Case statement. Use End Select to end the loop created.

Code:

Sub switch_case_example1()

Dim usrInpt As Integer
usrInpt = InputBox("Please enter your value")
Select Case usrInpt
Case Is < 100
MsgBox "The provided number is less than 100"
Case Is > 100
MsgBox "The provided number is greater than 100"
Case Is = 100
MsgBox "The provided number is 100"
End Select

End Sub

Use End Select Example 1-8

Step 10: Now, run this code by hitting the Run button or F5. A new popup box will appear asking for the value to be checked. I will enter a value as 110 and hit the OK button on the input box. Since the number I enter is greater than 100, as soon as I click on OK, a new message will pop-up on my screen saying, “The provided number is greater than 100”.

VBA Switch Case Output 1

VBA Switch Case – Example #2

Suppose we want to capture the grade of students based on their marks. See the steps below on how we can achieve this.

Step 1: Define a new sub-procedure named switch_case_example2.

Code:

Sub switch_case_example2()

End Sub

VBA Switch Case Example 2-1

Step 2: Define two new variables – marks as integer and grades as a string using Dim statement in the sub-procedure created.

Code:

Sub switch_case_example2()

Dim marks As Integer
Dim grades As String

End Sub

VBA Switch Case Example 2-2

Step 3: Use the VBA InputBox function through which the user can input the value for the variable marks.

Code:

Sub switch_case_example2()

Dim marks As Integer
Dim grades As String
marks = InputBox("Please enter the marks")

End Sub

VBA InputBox function Example 2-3

Step 4: Use the Select Case statement and supply the variable marks as an expression to be checked.

Code:

Sub switch_case_example2()

Dim marks As Integer
Dim grades As String
marks = InputBox("Please enter the marks")
Select Case marks

End Sub

VBA Switch Case Example 2-4

Since we want to assign the grades to students based on the marks they input, we need to define the grades first.

Step 5: Use the case statement to check whether the marks are less than 35. If so, assign value as “F” to variable grades.

Code:

Sub switch_case_example2()

Dim marks As Integer
Dim grades As String
marks = InputBox("Please enter the marks")
Select Case marks
Case Is < 35
grades = "F"

End Sub

VBA Switch Case Example 2-5

Step 6: If the marks are between 35 to 45, assign value as “D” to variable grades.

Code:

Sub switch_case_example2()

Dim marks As Integer
Dim grades As String
marks = InputBox("Please enter the marks")
Select Case marks
Case Is < 35
grades = "F"
Case 35 To 45
grades = "D"

End Sub

VBA Switch Case Example 2-6

Step 7: If the marks inputted are between 46 to 55, value “C” should get assigned to variable grades.

Code:

Sub switch_case_example2()

Dim marks As Integer
Dim grades As String
marks = InputBox("Please enter the marks")
Select Case marks
Case Is < 35
grades = "F"
Case 35 To 45
grades = "D"
Case 46 To 55
grades = "C"

End Sub

VBA Switch Case Example 2-7

Step 8: When the marks are in range 56 to 65, the value assigned to variable grades should be “B”.

Code:

Sub switch_case_example2()

Dim marks As Integer
Dim grades As String
marks = InputBox("Please enter the marks")
Select Case marks
Case Is < 35
grades = "F"
Case 35 To 45
grades = "D"
Case 46 To 55
grades = "C"
Case 56 To 65
grades = "B"

End Sub

VBA Switch Case Example 2-8

Step 9: For marks between 66 to 75, the grade should be “A”.

Code:

Sub switch_case_example2()

Dim marks As Integer
Dim grades As String
marks = InputBox("Please enter the marks")
Select Case marks
Case Is < 35
grades = "F"
Case 35 To 45
grades = "D"
Case 46 To 55
grades = "C"
Case 56 To 65
grades = "B"
Case 66 To 75
grades = "A"

End Sub

Marks between 66 to 75

Step 10: If the marks inputted are more than 75, grades variable should be assigned a value as “A+”

Code:

Sub switch_case_example2()

Dim marks As Integer
Dim grades As String
marks = InputBox("Please enter the marks")
Select Case marks
Case Is < 35
grades = "F"
Case 35 To 45
grades = "D"
Case 46 To 55
grades = "C"
Case 56 To 65
grades = "B"
Case 66 To 75
grades = "A"
Case Is > 75
grades = "A+"

End Sub

Marks More than 75

Step 11: Use End Select the close the Select Case loop.

Code:

Sub switch_case_example2()

Dim marks As Integer
Dim grades As String
marks = InputBox("Please enter the marks")
Select Case marks
Case Is < 35
grades = "F"
Case 35 To 45
grades = "D"
Case 46 To 55
grades = "C"
Case 56 To 65
grades = "B"
Case 66 To 75
grades = "A"
Case Is > 75
grades = "A+"
End Select

End Sub

Use End Select Example 2-11

Step 12: Now, we need to check what is the grade associated with the marks a student has inputted. In order to achieve that, use MsgBox function in such a way that it denotes the achieved grades inside a message box.

Code:

Sub switch_case_example2()

Dim marks As Integer
Dim grades As String
marks = InputBox("Please enter the marks")
Select Case marks
Case Is < 35
grades = "F"
Case 35 To 45
grades = "D"
Case 46 To 55
grades = "C"
Case 56 To 65
grades = "B"
Case 66 To 75
grades = "A"
Case Is > 75
grades = "A+"
End Select
MsgBox "Grade achieved is: " & grades

End Sub

Use MsgBox function Example 2-12

Step 13: Run this code by hitting the F5 or Run button and you can see a VBA input box asking for a mark value. I will enter the marks as 72 and hit the OK button present on the input box. As soon as I hit the OK button inside the input box, I will get a message box indicating the grades associated with the marks entered.

VBA Switch Case Output 2

This is how we can use the Switch/Select Case statement inside VBA.

Things to Remember

  • Switch case is actually a synonym for Select Case statement in VBA. It has been called so because with the help of this statement we can switch between the different outputs at the same time.
  • This statement can be used as an alternative to the IF-Else loop whenever we have three or more conditions to check.
  • It is OK if you don’t use ELSE Case in Select Case statement. However, in that case, you need to make sure that at least one condition is true throughout the loop.

Recommended Articles

This is a guide to VBA Switch Case. Here we discuss How to Use the Switch Case Statement in Excel VBA along with practical examples and downloadable excel template. You can also go through our other suggested articles –

  1. VBA IF Statements
  2. VBA Sort
  3. VBA While Loop
  4. VBA Environ
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign In
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Excel Charts
  • Excel Tips
  • All Tutorials
Certification Courses
  • All Courses
  • Excel VBA Course - All in One Bundle
  • VBA Course
  • Excel Data Analysis Course
  • Excel for Marketing Course
  • Excel for Finance Course
  • Excel for HR Training

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more