The Select Case statement in VBA is used to test a value against multiple possible cases. It is useful when a program needs to choose one action from several possible options.
For example, a student result program can use Select Case to display different grades based on marks, or a course-management program can use it to perform different actions based on a selected course.
Select Case is a VBA decision-making statement used to compare one expression with multiple possible values or conditions.
Basic structure:
Select Case expression
Case value1
statements
Case value2
statements
Case Else
statements
End Select
Select Case is useful when one value can have several possible outcomes.
For example:
The basic syntax is:
Select Case expression
Case value1
statement1
Case value2
statement2
Case Else
statement3
End Select
VBA evaluates the expression and then executes the matching Case.
Suppose a variable contains a number representing a day.
Dim dayNumber As Integer
dayNumber = 1
Select Case dayNumber
Case 1
MsgBox "Monday"
Case 2
MsgBox "Tuesday"
Case 3
MsgBox "Wednesday"
End Select
Since the value is 1, VBA executes Case 1.
The expression after Select Case is the value that VBA evaluates.
Select Case dayNumber
Here, dayNumber is the expression. VBA compares its value with each Case.
Select Case can compare numeric values.
Dim number As Integer
number = 2
Select Case number
Case 1
MsgBox "One"
Case 2
MsgBox "Two"
Case 3
MsgBox "Three"
Case Else
MsgBox "Other Number"
End Select
Select Case can also be used with text values.
Dim course As String
course = "ADCA"
Select Case course
Case "ADCA"
MsgBox "ADCA Course"
Case "Tally"
MsgBox "Tally Course"
Case "Python"
MsgBox "Python Course"
Case Else
MsgBox "Other Course"
End Select
Case Else executes when none of the other Case values match the expression.
Dim number As Integer
number = 10
Select Case number
Case 1
MsgBox "One"
Case 2
MsgBox "Two"
Case Else
MsgBox "Number not found"
End Select
You can use the value of an Excel cell as the Select Case expression.
Select Case Range("A2").Value
Case 1
Range("B2").Value = "Monday"
Case 2
Range("B2").Value = "Tuesday"
Case 3
Range("B2").Value = "Wednesday"
Case Else
Range("B2").Value = "Invalid Day"
End Select
Variables are commonly used with Select Case.
Dim marks As Integer
marks = 85
Select Case marks
Case 100
MsgBox "Perfect Score"
Case 85
MsgBox "Excellent"
Case 50
MsgBox "Average"
Case Else
MsgBox "Other Marks"
End Select
InputBox can collect a value from the user and Select Case can process it.
Dim choice As Integer
choice = CInt(InputBox("Enter 1, 2 or 3:"))
Select Case choice
Case 1
MsgBox "You selected Option 1"
Case 2
MsgBox "You selected Option 2"
Case 3
MsgBox "You selected Option 3"
Case Else
MsgBox "Invalid Option"
End Select
Select Case can be used to classify grades.
Dim grade As String
grade = "A"
Select Case grade
Case "A"
MsgBox "Excellent"
Case "B"
MsgBox "Very Good"
Case "C"
MsgBox "Good"
Case "D"
MsgBox "Needs Improvement"
Case Else
MsgBox "Invalid Grade"
End Select
A single Case can contain multiple values separated by commas.
Dim dayNumber As Integer
dayNumber = 6
Select Case dayNumber
Case 1, 2, 3, 4, 5
MsgBox "Weekday"
Case 6, 7
MsgBox "Weekend"
Case Else
MsgBox "Invalid Day"
End Select
Here, Cases 1 through 5 represent weekdays and Cases 6 and 7 represent weekends.
The To keyword can be used to specify a range of values.
Dim marks As Integer
marks = 75
Select Case marks
Case 80 To 100
MsgBox "Grade A"
Case 60 To 79
MsgBox "Grade B"
Case 40 To 59
MsgBox "Grade C"
Case 0 To 39
MsgBox "Fail"
Case Else
MsgBox "Invalid Marks"
End Select
A practical student result system can use ranges to determine grades.
Dim marks As Integer
marks = 82
Select Case marks
Case 80 To 100
MsgBox "Grade A"
Case 60 To 79
MsgBox "Grade B"
Case 40 To 59
MsgBox "Grade C"
Case Else
MsgBox "Fail"
End Select
The Is keyword can be used with comparison operators in a Case statement.
Dim marks As Integer
marks = 85
Select Case marks
Case Is >= 80
MsgBox "Grade A"
Case Is >= 60
MsgBox "Grade B"
Case Is >= 40
MsgBox "Grade C"
Case Else
MsgBox "Fail"
End Select
This allows Select Case to work with comparison conditions.
Select Case can classify people into age groups.
Dim age As Integer
age = 25
Select Case age
Case 0 To 12
MsgBox "Child"
Case 13 To 19
MsgBox "Teenager"
Case 20 To 59
MsgBox "Adult"
Case Is >= 60
MsgBox "Senior Citizen"
Case Else
MsgBox "Invalid Age"
End Select
Select Case is useful for creating simple menu-driven programs.
Dim choice As Integer
choice = CInt(InputBox("Enter 1, 2 or 3:"))
Select Case choice
Case 1
MsgBox "Add Student"
Case 2
MsgBox "View Student"
Case 3
MsgBox "Delete Student"
Case Else
MsgBox "Invalid Choice"
End Select
A month number can be converted into a month name using Select Case.
Dim monthNumber As Integer
monthNumber = 4
Select Case monthNumber
Case 1
MsgBox "January"
Case 2
MsgBox "February"
Case 3
MsgBox "March"
Case 4
MsgBox "April"
Case 5
MsgBox "May"
Case Else
MsgBox "Other Month"
End Select
Select Case can classify attendance percentages.
Dim attendance As Double
attendance = 82
Select Case attendance
Case 90 To 100
MsgBox "Excellent Attendance"
Case 75 To 89
MsgBox "Good Attendance"
Case 60 To 74
MsgBox "Average Attendance"
Case 0 To 59
MsgBox "Low Attendance"
Case Else
MsgBox "Invalid Attendance"
End Select
You can use Select Case to classify fee payment amounts.
Dim paid As Double
paid = 5000
Select Case paid
Case 0
MsgBox "Fee Pending"
Case 1 To 4999
MsgBox "Partial Payment"
Case 5000 To 9999
MsgBox "More Payment Required"
Case Is >= 10000
MsgBox "Fee Fully Paid"
End Select
Each Case can contain multiple VBA statements.
Dim marks As Integer
marks = 85
Select Case marks
Case Is >= 80
Range("C2").Value = "A"
Range("D2").Value = "Excellent"
MsgBox "Grade A"
Case Is >= 60
Range("C2").Value = "B"
Range("D2").Value = "Very Good"
MsgBox "Grade B"
Case Else
Range("C2").Value = "C"
Range("D2").Value = "Needs Improvement"
MsgBox "Other Grade"
End Select
The value from a worksheet can be used directly.
Select Case Range("B2").Value
Case Is >= 80
Range("C2").Value = "A"
Case Is >= 60
Range("C2").Value = "B"
Case Is >= 40
Range("C2").Value = "C"
Case Else
Range("C2").Value = "Fail"
End Select
This is useful for automated Excel result systems.
Both Select Case and ElseIf can be used for multiple decisions, but their structures are different.
ElseIf:
If marks >= 80 Then
MsgBox "A"
ElseIf marks >= 60 Then
MsgBox "B"
Else
MsgBox "C"
End If
Select Case:
Select Case marks
Case Is >= 80
MsgBox "A"
Case Is >= 60
MsgBox "B"
Case Else
MsgBox "C"
End Select
Select Case can make some multiple-choice logic easier to read.
The following program takes marks from the user and automatically assigns a grade.
Dim marks As Double
Dim grade As String
marks = CDbl(InputBox("Enter student marks:"))
Select Case marks
Case 80 To 100
grade = "A"
Case 60 To 79
grade = "B"
Case 40 To 59
grade = "C"
Case 0 To 39
grade = "Fail"
Case Else
grade = "Invalid Marks"
End Select
Range("B2").Value = marks
Range("C2").Value = grade
MsgBox "Grade: " & grade
Beginners commonly make these mistakes:
Correct structure:
Select Case value
Case 1
MsgBox "One"
Case 2
MsgBox "Two"
Case Else
MsgBox "Other"
End Select
Select Case can be used to create a simple student-management menu.
Dim choice As Integer
choice = CInt(InputBox( _
"1 - Add Student" & vbCrLf & _
"2 - View Student" & vbCrLf & _
"3 - Exit" & vbCrLf & _
"Enter your choice:"))
Select Case choice
Case 1
MsgBox "Add Student Selected"
Case 2
MsgBox "View Student Selected"
Case 3
MsgBox "Exit Selected"
Case Else
MsgBox "Invalid Choice"
End Select
A typical Select Case workflow is:
Dim choice As Integer
choice = 2
Select Case choice
Case 1
MsgBox "Option 1"
Case 2
MsgBox "Option 2"
Case 3
MsgBox "Option 3"
Case Else
MsgBox "Invalid Option"
End Select
The Select Case statement is an important VBA decision-making tool. It allows one expression to be compared with multiple values, ranges, or conditions.
It is especially useful for student result systems, menus, course selection, attendance classification, fee status, and other Excel automation projects.
Dim marks As Double
Dim grade As String
marks = CDbl(InputBox("Enter student marks:"))
Select Case marks
Case 80 To 100
grade = "A"
Case 60 To 79
grade = "B"
Case 40 To 59
grade = "C"
Case 0 To 39
grade = "Fail"
Case Else
grade = "Invalid Marks"
End Select
Range("B2").Value = marks
Range("C2").Value = grade
MsgBox "Grade: " & grade
The next lesson will introduce the For...Next loop, which is used to repeat a block of VBA code a specific number of times.
Question: Which keyword is used to define the possible values in a Select Case statement?