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.
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
If...Then...Else is useful when there are two possible outcomes.
For example:
The basic syntax is:
If condition Then
statement1
Else
statement2
End If
If the condition is true, statement1 runs. Otherwise, statement2 runs.
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.
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.
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.
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.
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.
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
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.
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
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
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
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
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.
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
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
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
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
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
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
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
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
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
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
Beginners commonly make these mistakes:
Correct structure:
If condition Then
statements
Else
statements
End If
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
Testing both outcomes helps ensure that the program behaves correctly.
A typical workflow is:
Dim marks As Integer
marks = 55
If marks >= 40 Then
MsgBox "Pass"
Else
MsgBox "Fail"
End If
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.
Question: Which block of an If...Then...Else statement executes when the condition is false?