Lesson 32 of 60 – If...Then...Else in VBA
53%

If...Then...Else in VBA

The If...Then...Else statement is used when a VBA program needs to choose between two different actions.

If the condition is True, VBA executes the code inside the If block. If the condition is False, VBA executes the code inside the Else block.

Note: Use If...Then...Else when you want your program to perform one action when a condition is true and another action when the condition is false.

1. What is If...Then...Else?

If...Then...Else is a VBA decision-making statement. It allows a program to execute different code depending on whether a condition is true or false.

Basic structure:

If condition Then
    statements
Else
    statements
End If

2. Why Use If...Then...Else?

If...Then...Else is useful when there are two possible outcomes.

For example:

  • Student Pass or Fail
  • Eligible or Not Eligible
  • Paid or Unpaid
  • Available or Not Available
  • Adult or Minor

3. Basic Syntax

The basic syntax is:

If condition Then
    statement1
Else
    statement2
End If

If the condition is true, statement1 runs. Otherwise, statement2 runs.

4. Simple If...Then...Else Example

Suppose we want to check whether a student has passed.

Dim marks As Integer

marks = 65

If marks >= 40 Then
    MsgBox "Pass"
Else
    MsgBox "Fail"
End If

Because the marks are 65, the condition is true and Pass is displayed.

5. Understanding the Condition

The condition is the expression that VBA checks.

If marks >= 40 Then

If the value of marks is 40 or greater, the condition is True. Otherwise, it is False.

6. If Condition is True

When the condition is true, VBA executes the statements between If and Else.

Dim age As Integer

age = 25

If age >= 18 Then
    MsgBox "Adult"
Else
    MsgBox "Minor"
End If

Since 25 is greater than or equal to 18, Adult is displayed.

7. If Condition is False

When the condition is false, VBA skips the If block and executes the Else block.

Dim age As Integer

age = 15

If age >= 18 Then
    MsgBox "Adult"
Else
    MsgBox "Minor"
End If

Because 15 is less than 18, Minor is displayed.

8. If...Then...Else with Excel Cells

You can use a cell value as the condition.

If Range("B2").Value >= 40 Then
    Range("C2").Value = "Pass"
Else
    Range("C2").Value = "Fail"
End If

The result is written to cell C2.

9. If...Then...Else with Variables

Variables can be used in conditional statements.

Dim salary As Double

salary = 18000

If salary >= 20000 Then
    MsgBox "Salary is high"
Else
    MsgBox "Salary is below 20000"
End If

10. If...Then...Else with Text

Text values can also be compared.

Dim course As String

course = "ADCA"

If course = "ADCA" Then
    MsgBox "ADCA Selected"
Else
    MsgBox "Other Course Selected"
End If

Text values are written inside quotation marks.

11. If...Then...Else with InputBox

InputBox can be used to get a value from the user and then check it.

Dim marks As Integer

marks = CInt(InputBox("Enter marks:"))

If marks >= 40 Then
    MsgBox "Pass"
Else
    MsgBox "Fail"
End If

12. If...Then...Else with MsgBox

MsgBox can be used inside both branches.

Dim number As Integer

number = 10

If number > 0 Then
    MsgBox "Positive Number"
Else
    MsgBox "Zero or Negative Number"
End If

13. Checking Even and Odd Numbers

The Mod operator can be used with If...Then...Else to check whether a number is even or odd.

Dim number As Integer

number = 8

If number Mod 2 = 0 Then
    MsgBox "Even Number"
Else
    MsgBox "Odd Number"
End If

14. Checking Positive and Negative Numbers

You can use If...Then...Else to classify a number.

Dim number As Integer

number = -5

If number >= 0 Then
    MsgBox "Positive Number"
Else
    MsgBox "Negative Number"
End If

15. Student Pass or Fail

One of the most common examples is checking a student's result.

Dim marks As Double

marks = 35

If marks >= 40 Then
    MsgBox "Student Passed"
Else
    MsgBox "Student Failed"
End If

If marks are less than 40, the Else block executes.

16. Attendance Check

You can check whether a student's attendance meets a required percentage.

Dim attendance As Double

attendance = 68

If attendance >= 75 Then
    MsgBox "Attendance Requirement Completed"
Else
    MsgBox "Attendance is Low"
End If

17. Fee Payment Check

If...Then...Else can be used to check whether a fee has been paid.

Dim feePaid As Double

feePaid = 0

If feePaid > 0 Then
    MsgBox "Fee Paid"
Else
    MsgBox "Fee Pending"
End If

18. Multiple Statements in If Block

You can execute multiple statements when the condition is true or false.

Dim marks As Integer

marks = 75

If marks >= 40 Then

    Range("B2").Value = "Pass"
    Range("C2").Value = marks
    MsgBox "Result Saved"

Else

    Range("B2").Value = "Fail"
    Range("C2").Value = marks
    MsgBox "Result Saved"

End If

19. Using Greater Than Operator

The greater than operator > can be used to compare values.

Dim marks As Integer

marks = 75

If marks > 50 Then
    MsgBox "Marks are greater than 50"
Else
    MsgBox "Marks are 50 or below"
End If

20. Using Less Than Operator

The less than operator < can also be used.

Dim age As Integer

age = 16

If age < 18 Then
    MsgBox "Minor"
Else
    MsgBox "Adult"
End If

21. Using Not Equal Operator

The <> operator checks whether two values are different.

Dim status As String

status = "Pending"

If status <> "Paid" Then
    MsgBox "Payment is not complete"
Else
    MsgBox "Payment is complete"
End If

22. Using And with If...Then...Else

The And operator allows you to check multiple conditions. Both conditions must be true.

Dim marks As Integer

marks = 75

If marks >= 40 And marks <= 100 Then
    MsgBox "Valid Marks"
Else
    MsgBox "Invalid Marks"
End If

23. Using Or with If...Then...Else

The Or operator allows the condition to be true when at least one condition is true.

Dim course As String

course = "Tally"

If course = "ADCA" Or course = "Tally" Then
    MsgBox "Course Available"
Else
    MsgBox "Course Not Available"
End If

24. If...Then...Else with Boolean Values

Boolean variables contain True or False.

Dim isPaid As Boolean

isPaid = False

If isPaid = True Then
    MsgBox "Fee Paid"
Else
    MsgBox "Fee Pending"
End If

25. Practical Student Result Example

The following example gets marks from the user and stores the result in Excel.

Dim marks As Double

marks = CDbl(InputBox("Enter student marks:"))

Range("B2").Value = marks

If marks >= 40 Then

    Range("C2").Value = "Pass"
    MsgBox "Student Passed"

Else

    Range("C2").Value = "Fail"
    MsgBox "Student Failed"

End If

26. Common Mistakes with If...Then...Else

Beginners commonly make these mistakes:

  • Forgetting the Then keyword.
  • Forgetting End If.
  • Writing Else inside the wrong position.
  • Using incorrect comparison operators.
  • Forgetting quotation marks around text.
  • Using the wrong variable data type.

Correct structure:

If condition Then
    statements
Else
    statements
End If

27. Practical Data Validation Example

If...Then...Else can be used to validate user input.

Dim name As String

name = InputBox("Enter student name:")

If name = "" Then

    MsgBox "Please enter a student name."

Else

    Range("A2").Value = name
    MsgBox "Student name saved."

End If

28. Best Practices for If...Then...Else

  • Keep conditions clear and readable.
  • Use meaningful variable names.
  • Indent code inside the If and Else blocks.
  • Use the correct comparison operators.
  • Test both true and false conditions.
  • Use comments when conditions are complex.

Testing both outcomes helps ensure that the program behaves correctly.

29. Complete If...Then...Else Workflow

A typical workflow is:

  1. Declare a variable.
  2. Get or assign a value.
  3. Write the condition.
  4. Write the statements for the true condition.
  5. Add the Else block.
  6. Write the statements for the false condition.
  7. Close the block using End If.
  8. Test both possible outcomes.
Dim marks As Integer

marks = 55

If marks >= 40 Then
    MsgBox "Pass"
Else
    MsgBox "Fail"
End If

30. Complete Understanding of If...Then...Else

The If...Then...Else statement is used when a VBA program needs to choose between two possible actions.

The If block executes when the condition is true, while the Else block executes when the condition is false.

Dim marks As Double

marks = CDbl(InputBox("Enter marks:"))

If marks >= 40 Then

    Range("B2").Value = marks
    Range("C2").Value = "Pass"
    MsgBox "Student Passed"

Else

    Range("B2").Value = marks
    Range("C2").Value = "Fail"
    MsgBox "Student Failed"

End If

This concept is important for practical VBA applications such as student result systems, attendance systems, fee management, data validation, and report generation.

📌 Key Points

  • If...Then...Else is used for two-way decision making.
  • The If block runs when the condition is true.
  • The Else block runs when the condition is false.
  • The Then keyword follows the condition.
  • End If closes a multi-line conditional block.
  • If...Then...Else can work with variables and Excel cells.
  • InputBox can provide values for conditions.
  • Both true and false conditions should be tested.

🧠 Quick Quiz

Question: Which block of an If...Then...Else statement executes when the condition is false?