Lesson 14: Select Case in VB2019
Master efficient multi-condition handling with Select Case
Key Takeaway
The Select Case structure provides a cleaner and more efficient way to handle multiple conditions compared to complex If...Then...ElseIf statements.
In this lesson, we'll explore the Select Case control structure in Visual Basic 2019. While If...Then...Else is excellent for binary decisions, Select Case excels when you need to evaluate a single expression against multiple possible values.
14.1 Select Case Structure
The Select Case structure evaluates one expression and executes different code blocks based on matching values. It's ideal for scenarios with multiple discrete values to check.
Select Case Syntax
Select Case expression Case value1 ' Code to execute when expression = value1 Case value2 ' Code to execute when expression = value2 Case value3 ' Code to execute when expression = value3 Case Else ' Code to execute when no match is found End Select
14.1(a) Basic Select Case
This structure executes specific code blocks based on exact matches with the expression value.
Example 14.1: Grade Messages
This program displays a message associated with a grade entered by the user.
Private Sub BtnShow_Click(sender As Object, e As EventArgs) Handles BtnShow.Click Dim grade As String grade = TxtGrade.Text Select Case grade Case "A" MsgBox("High Distinction") Case "A-" MsgBox("Distinction") Case "B" MsgBox("Credit") Case "C" MsgBox("Pass") Case Else MsgBox("Fail") End Select End Sub
Output:


14.1(b) Using Comparison Operators
You can use the Is keyword with comparison operators to evaluate ranges of values.
Example 14.2: Mark Evaluation
This program evaluates a mark and provides feedback using comparison operators.
Private Sub BtnEvaluate_Click(sender As Object, e As EventArgs) Handles BtnEvaluate.Click Dim mark As Single mark = TxtMark.Text Select Case mark Case Is >= 85 MsgBox("Excellence") Case Is >= 70 MsgBox("Good") Case Is >= 60 MsgBox("Above Average") Case Is >= 50 MsgBox("Average") Case Else MsgBox("Need to work harder") End Select End Sub
Output:
14.1(c) Using Value Ranges
Select Case allows you to specify ranges of values using the To keyword for more concise code.
Example 14.3: Mark Ranges
This example demonstrates using value ranges for more concise code.
Private Sub BtnEvaluate_Click(sender As Object, e As EventArgs) Handles BtnEvaluate.Click Dim mark As Single mark = TxtMark.Text Select Case mark Case 0 To 49 MsgBox("Need to work harder") Case 50 To 59 MsgBox("Average") Case 60 To 69 MsgBox("Above Average") Case 70 To 84 MsgBox("Good") Case 85 To 100 MsgBox("Excellence") Case Else MsgBox("Wrong entry, please reenter the mark") End Select End Sub
Output:
14.1(d) Calculating Grades
Select Case is ideal for converting numerical marks to letter grades.
Example 14.4: Grade Calculator
This program calculates a letter grade based on a numerical mark.
Private Sub BtnCalculate_Click(sender As Object, e As EventArgs) Handles BtnCalculate.Click Dim mark As Single Dim grade As String mark = TxtMark.Text Select Case mark Case 0 To 49 grade = "E" Case 50 To 59 grade = "D" Case 60 To 69 grade = "C" Case 70 To 79 grade = "B" Case 80 To 100 grade = "A" Case Else grade = "Error, please re-enter the mark" End Select LblGrade.Text = grade End Sub
Output:

Lesson Summary
In this lesson, you've learned how to use the Select Case control structure for efficient multi-condition handling in Visual Basic 2019:
Select Case Structure
Mastered the fundamental Select Case...End Select syntax
Comparison Operators
Used the "Is" keyword with operators for range evaluations
Value Ranges
Implemented value ranges using the "To" keyword for concise code
Practical Applications
Built grade calculators and mark evaluation systems
Select Case provides a cleaner alternative to complex If...Then...ElseIf structures when evaluating a single expression against multiple possible values. In the next lesson, we'll explore looping structures to repeat code execution.
Next Lesson
Ready to learn about repeating code execution? Continue to Lesson 15: Looping.
Related Resources

Visual Basic 2019 Made Easy
Unlock the power of Visual Basic 2019 with this comprehensive, easy-to-follow handbook written by Dr. Liew, renowned educator and founder of the popular programming tutorial website VBtutor.net. Whether you're new to programming or brushing up your skills, this book is your perfect companion to learn Visual Basic 2019 from the ground up.
What You'll Learn:
- Understand Core Programming Concepts: Grasp the foundational principles of Visual Basic 2019, including variables, data types, conditional logic, loops, and event-driven programming.
- Develop Real Windows Desktop Applications: Build fully functional and interactive Windows apps using Visual Studio 2019—guided through step-by-step tutorials.
- Apply Dozens of Ready-to-Use Examples: Explore a rich collection of practical sample programs, from basic calculators to image viewers and database applications.
- Adapt and Reuse Code for Your Own Projects: Customize professionally written code snippets to speed up your development process and bring your ideas to life.
- Package and Deploy Like a Pro: Learn how to compile, test, and distribute your Visual Basic applications seamlessly with built-in deployment tools.

Visual Basic Programming With Code Examples
Visual Basic Programming with Code Examples offers a unique dual-format approach, showcasing sample codes in both Visual Basic 6 (VB6) and VB.NET. This side-by-side presentation helps you understand the evolution of Visual Basic and empowers you to work confidently across both environments.
What You'll Learn:
- Core Concepts Made Easy: Explore data types, control structures, file handling, procedures, user interface design, and more.
- Hands-On Application Building: Design real-world applications, including financial calculators, educational tools, games, multimedia apps, and database systems.
- 48 Practical Code Examples: Study and customize fully explained programs that illustrate key programming techniques.
- Dual-Code Format: Learn to translate and adapt code between VB6 and VB.NET seamlessly.