The ElseIf statement in VBA is used when a program needs to check multiple conditions. It allows VBA to test one condition after another and execute the code belonging to the first condition that is true.
For example, in a student result system, you can use ElseIf to assign different grades based on marks.
ElseIf is used inside an If statement to check an additional condition when the previous condition is false.
Basic structure:
If condition1 Then
statements
ElseIf condition2 Then
statements
End If
ElseIf is useful when a program has several possible results.
For example:
Instead of creating many separate If statements, you can use one If...ElseIf structure.
The basic syntax is:
If condition1 Then
statement1
ElseIf condition2 Then
statement2
End If
VBA checks the first condition. If it is false, it checks the ElseIf condition.
You can use more than one ElseIf statement.
Dim marks As Integer
marks = 75
If marks >= 80 Then
MsgBox "Grade A"
ElseIf marks >= 60 Then
MsgBox "Grade B"
ElseIf marks >= 40 Then
MsgBox "Grade C"
End If
The order of conditions is important. VBA checks conditions from top to bottom.
If marks >= 80 Then
MsgBox "Grade A"
ElseIf marks >= 60 Then
MsgBox "Grade B"
ElseIf marks >= 40 Then
MsgBox "Grade C"
End If
For marks of 85, VBA executes the first condition and does not continue checking the remaining ElseIf conditions.
An Else block can be added after all ElseIf conditions. It executes when none of the conditions are true.
If marks >= 80 Then
MsgBox "Grade A"
ElseIf marks >= 60 Then
MsgBox "Grade B"
ElseIf marks >= 40 Then
MsgBox "Grade C"
Else
MsgBox "Fail"
End If
A common practical use of ElseIf is calculating student grades.
Dim marks As Double
marks = 72
If marks >= 80 Then
MsgBox "Grade A"
ElseIf marks >= 60 Then
MsgBox "Grade B"
ElseIf marks >= 40 Then
MsgBox "Grade C"
Else
MsgBox "Fail"
End If
You can use a worksheet cell as the value for ElseIf conditions.
If Range("B2").Value >= 80 Then
Range("C2").Value = "A"
ElseIf Range("B2").Value >= 60 Then
Range("C2").Value = "B"
ElseIf Range("B2").Value >= 40 Then
Range("C2").Value = "C"
Else
Range("C2").Value = "Fail"
End If
Variables can be used to make multiple decisions.
Dim age As Integer
age = 25
If age < 13 Then
MsgBox "Child"
ElseIf age < 18 Then
MsgBox "Teenager"
ElseIf age < 60 Then
MsgBox "Adult"
Else
MsgBox "Senior Citizen"
End If
ElseIf can compare text values.
Dim course As String
course = "Tally"
If course = "ADCA" Then
MsgBox "ADCA Course"
ElseIf course = "Tally" Then
MsgBox "Tally Course"
ElseIf course = "Python" Then
MsgBox "Python Course"
Else
MsgBox "Other Course"
End If
InputBox can be used to get a value from the user and ElseIf can process it.
Dim marks As Double
marks = CDbl(InputBox("Enter marks:"))
If marks >= 80 Then
MsgBox "Excellent"
ElseIf marks >= 60 Then
MsgBox "Good"
ElseIf marks >= 40 Then
MsgBox "Pass"
Else
MsgBox "Fail"
End If
MsgBox can display a different message for each condition.
Dim number As Integer
number = 0
If number > 0 Then
MsgBox "Positive"
ElseIf number < 0 Then
MsgBox "Negative"
Else
MsgBox "Zero"
End If
ElseIf can classify attendance into different categories.
Dim attendance As Double
attendance = 82
If attendance >= 90 Then
MsgBox "Excellent Attendance"
ElseIf attendance >= 75 Then
MsgBox "Good Attendance"
ElseIf attendance >= 60 Then
MsgBox "Average Attendance"
Else
MsgBox "Low Attendance"
End If
You can classify fee payment into different categories.
Dim paid As Double
paid = 5000
If paid >= 10000 Then
MsgBox "Fee Fully Paid"
ElseIf paid > 0 Then
MsgBox "Partial Payment"
Else
MsgBox "Fee Pending"
End If
ElseIf can be used to classify people into age groups.
Dim age As Integer
age = 16
If age <= 12 Then
MsgBox "Child"
ElseIf age <= 18 Then
MsgBox "Teenager"
ElseIf age <= 59 Then
MsgBox "Adult"
Else
MsgBox "Senior"
End If
There is no requirement to use only one ElseIf. Multiple ElseIf statements can be used when there are many possible outcomes.
If marks >= 90 Then
MsgBox "Grade A+"
ElseIf marks >= 80 Then
MsgBox "Grade A"
ElseIf marks >= 70 Then
MsgBox "Grade B"
ElseIf marks >= 60 Then
MsgBox "Grade C"
ElseIf marks >= 40 Then
MsgBox "Grade D"
Else
MsgBox "Fail"
End If
Each condition can contain multiple statements.
If marks >= 40 Then
Range("C2").Value = "Pass"
Range("D2").Value = "Eligible"
MsgBox "Student Passed"
Else
Range("C2").Value = "Fail"
Range("D2").Value = "Not Eligible"
MsgBox "Student Failed"
End If
The And operator can be used when an ElseIf condition requires multiple conditions to be true.
If marks >= 40 And marks <= 100 Then
MsgBox "Valid Marks"
ElseIf marks < 0 Then
MsgBox "Invalid Marks"
Else
MsgBox "Check Marks"
End If
The Or operator can combine alternative conditions.
Dim course As String
course = "Tally"
If course = "ADCA" Or course = "Tally" Then
MsgBox "Basic Course"
ElseIf course = "Python" Or course = "Java" Then
MsgBox "Programming Course"
Else
MsgBox "Other Course"
End If
VBA executes the first condition that evaluates to True and then skips the remaining ElseIf conditions.
Dim marks As Integer
marks = 85
If marks >= 80 Then
MsgBox "Grade A"
ElseIf marks >= 60 Then
MsgBox "Grade B"
ElseIf marks >= 40 Then
MsgBox "Grade C"
End If
The result is Grade A. The other conditions are not executed.
Conditions should normally be arranged carefully. For grade calculations, higher ranges are commonly checked before lower ranges.
Example:
If marks >= 80 Then
MsgBox "A"
ElseIf marks >= 60 Then
MsgBox "B"
ElseIf marks >= 40 Then
MsgBox "C"
Else
MsgBox "Fail"
End If
ElseIf can be used to assign different discounts based on purchase amount.
Dim amount As Double
amount = 15000
If amount >= 20000 Then
MsgBox "20% Discount"
ElseIf amount >= 10000 Then
MsgBox "10% Discount"
ElseIf amount >= 5000 Then
MsgBox "5% Discount"
Else
MsgBox "No Discount"
End If
ElseIf can classify performance into different levels.
Dim score As Integer
score = 78
If score >= 90 Then
MsgBox "Outstanding"
ElseIf score >= 75 Then
MsgBox "Very Good"
ElseIf score >= 60 Then
MsgBox "Good"
ElseIf score >= 40 Then
MsgBox "Average"
Else
MsgBox "Needs Improvement"
End If
You can create a simple result system using an Excel cell.
Dim marks As Double
marks = Range("B2").Value
If marks >= 80 Then
Range("C2").Value = "A"
ElseIf marks >= 60 Then
Range("C2").Value = "B"
ElseIf marks >= 40 Then
Range("C2").Value = "C"
Else
Range("C2").Value = "Fail"
End If
The following example takes marks from the user and saves the grade in Excel.
Dim marks As Double
Dim grade As String
marks = CDbl(InputBox("Enter student marks:"))
If marks >= 80 Then
grade = "A"
ElseIf marks >= 60 Then
grade = "B"
ElseIf marks >= 40 Then
grade = "C"
Else
grade = "Fail"
End If
Range("B2").Value = marks
Range("C2").Value = grade
MsgBox "Result saved successfully."
Beginners commonly make these mistakes:
Correct syntax:
ElseIf condition Then
ElseIf can be used to validate a number entered by the user.
Dim marks As Double
marks = CDbl(InputBox("Enter marks:"))
If marks < 0 Then
MsgBox "Marks cannot be negative."
ElseIf marks > 100 Then
MsgBox "Marks cannot be greater than 100."
ElseIf marks >= 40 Then
MsgBox "Student Passed."
Else
MsgBox "Student Failed."
End If
For example, if 40 is the passing mark, test values such as 39, 40, and 41.
A typical ElseIf workflow is:
Dim marks As Integer
marks = 68
If marks >= 80 Then
MsgBox "Grade A"
ElseIf marks >= 60 Then
MsgBox "Grade B"
ElseIf marks >= 40 Then
MsgBox "Grade C"
Else
MsgBox "Fail"
End If
The ElseIf statement allows VBA to check multiple conditions in sequence. VBA starts with the If condition and then checks each ElseIf condition until it finds the first condition that is True.
If none of the conditions are true, the optional Else block is executed.
Dim marks As Double
Dim grade As String
marks = CDbl(InputBox("Enter student marks:"))
If marks >= 80 Then
grade = "A"
ElseIf marks >= 60 Then
grade = "B"
ElseIf marks >= 40 Then
grade = "C"
Else
grade = "Fail"
End If
Range("B2").Value = marks
Range("C2").Value = grade
MsgBox "Grade: " & grade
ElseIf is especially useful in practical VBA projects such as student result systems, attendance classification, fee status, discount calculations, and data validation.
Question: What is the main purpose of the ElseIf statement in VBA?