Lesson 33 of 60 – ElseIf Statement in VBA
55%

ElseIf Statement in VBA

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.

Note: Use ElseIf when you have more than two possible outcomes. For only two outcomes, If...Then...Else is usually sufficient.

1. What is ElseIf?

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

2. Why Use ElseIf?

ElseIf is useful when a program has several possible results.

For example:

  • Grade A
  • Grade B
  • Grade C
  • Grade D
  • Fail

Instead of creating many separate If statements, you can use one If...ElseIf structure.

3. Basic ElseIf Syntax

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.

4. ElseIf with Three Conditions

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

5. Understanding Condition Order

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.

6. ElseIf with an Else Block

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

7. ElseIf for Student Grades

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

8. ElseIf with Excel Cells

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

9. ElseIf with Variables

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

10. ElseIf with Text

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

11. ElseIf with InputBox

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

12. ElseIf with MsgBox

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

13. ElseIf for Attendance

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

14. ElseIf for Fee Status

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

15. ElseIf for Age Groups

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

16. Multiple ElseIf Statements

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

17. ElseIf with Multiple Statements

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

18. ElseIf with And Operator

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

19. ElseIf with Or Operator

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

20. ElseIf and the First True Condition

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.

21. Importance of Condition Order

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

22. ElseIf for Discount Calculation

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

23. ElseIf for Performance Levels

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

24. ElseIf with Excel Result System

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

25. Practical Student Grade Example

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."

26. Common Mistakes with ElseIf

Beginners commonly make these mistakes:

  • Writing Else If incorrectly instead of VBA's ElseIf.
  • Forgetting the Then keyword.
  • Forgetting End If.
  • Using conditions in the wrong order.
  • Forgetting quotation marks around text.
  • Adding unnecessary separate If statements.

Correct syntax:

ElseIf condition Then

27. Practical Input Validation Example

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

28. Best Practices for ElseIf

  • Write conditions in a logical order.
  • Keep each condition easy to understand.
  • Use meaningful variable names.
  • Indent the statements inside each block.
  • Use Else for the remaining possibilities when appropriate.
  • Test values at the boundaries of each condition.

For example, if 40 is the passing mark, test values such as 39, 40, and 41.

29. Complete ElseIf Workflow

A typical ElseIf workflow is:

  1. Declare a variable.
  2. Get or assign a value.
  3. Write the first condition.
  4. Add ElseIf for additional conditions.
  5. Add Else for the remaining case if required.
  6. Close the structure with End If.
  7. Test different values.
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

30. Complete Understanding of ElseIf

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.

📌 Key Points

  • ElseIf is used to check multiple conditions.
  • VBA checks conditions from top to bottom.
  • The first True condition is executed.
  • Remaining ElseIf conditions are skipped after a True condition is found.
  • An optional Else block handles all remaining cases.
  • ElseIf can work with variables and Excel cells.
  • ElseIf can be combined with And and Or.
  • Condition order is important.
  • ElseIf is useful for grades, categories, validation, and classifications.

🧠 Quick Quiz

Question: What is the main purpose of the ElseIf statement in VBA?