The If...Then statement is used in VBA to make decisions. It allows a program to execute a block of code only when a specified condition is true.
For example, a student result program can use an If...Then statement to check whether marks are greater than or equal to 40.
If...Then is a conditional statement in VBA. It checks a condition and executes code when that condition is true.
Basic structure:
If condition Then
statement
End If
The code between If and End If runs only when the condition is true.
If...Then is used when a program needs to make a decision.
For example:
The basic syntax is:
If condition Then
statement
End If
Example:
If marks >= 40 Then
MsgBox "Pass"
End If
If marks are 40 or greater, the message Pass will be displayed.
Suppose a variable contains a student's marks.
Dim marks As Integer
marks = 75
If marks >= 40 Then
MsgBox "Student Passed"
End If
Because 75 is greater than 40, the message is displayed.
The condition is the expression that VBA checks.
Example:
If marks >= 40 Then
Here marks >= 40 is the condition. It can be either True or False.
The greater than operator > can be used with If...Then.
Dim marks As Integer
marks = 75
If marks > 50 Then
MsgBox "Marks are greater than 50"
End If
The less than operator < can also be used.
Dim age As Integer
age = 15
If age < 18 Then
MsgBox "Age is below 18"
End If
The equal to operator = checks whether two values are equal.
Dim marks As Integer
marks = 40
If marks = 40 Then
MsgBox "Marks are exactly 40"
End If
The <> operator means not equal to.
Dim marks As Integer
marks = 55
If marks <> 40 Then
MsgBox "Marks are not 40"
End If
You can use an Excel cell as the condition.
If Range("B2").Value >= 40 Then
MsgBox "Pass"
End If
The value in cell B2 is checked against 40.
Variables are commonly used with If...Then.
Dim salary As Double
salary = 25000
If salary > 20000 Then
MsgBox "Salary is above 20000"
End If
If...Then can also compare text values.
Dim course As String
course = "ADCA"
If course = "ADCA" Then
MsgBox "ADCA Course Selected"
End If
Text values are normally written inside quotation marks.
InputBox can collect information from the user and If...Then can check it.
Dim marks As Integer
marks = CInt(InputBox("Enter marks:"))
If marks >= 40 Then
MsgBox "Pass"
End If
MsgBox can be used inside an If...Then block to display a message.
Dim age As Integer
age = 20
If age >= 18 Then
MsgBox "You are an adult."
End If
A common practical example is checking whether a student has passed.
Dim marks As Double
marks = 65
If marks >= 40 Then
MsgBox "Student Passed"
End If
If marks are below 40, nothing is displayed because there is no Else block.
If...Then can be used to check attendance percentage.
Dim attendance As Double
attendance = 80
If attendance >= 75 Then
MsgBox "Attendance Requirement Completed"
End If
You can check whether a fee amount has been received.
Dim fee As Double
fee = 5000
If fee > 0 Then
MsgBox "Fee Payment Received"
End If
An If...Then block can contain multiple statements.
Dim marks As Integer
marks = 80
If marks >= 40 Then
Range("B2").Value = "Pass"
Range("C2").Value = marks
MsgBox "Result Saved"
End If
All statements inside the block execute when the condition is true.
You can perform calculations inside an If...Then block.
Dim marks As Double
Dim percentage As Double
marks = 450
If marks >= 400 Then
percentage = marks / 5
MsgBox "Percentage = " & percentage
End If
If...Then can check a cell and then modify another cell.
If Range("B2").Value >= 40 Then
Range("C2").Value = "Pass"
End If
If B2 contains 40 or more, C2 receives the value Pass.
Multiple conditions can be combined using logical operators such as And.
Dim marks As Integer
marks = 75
If marks >= 40 And marks <= 100 Then
MsgBox "Valid Passing Marks"
End If
Both conditions must be true.
The Or operator allows a condition to be true when at least one condition is true.
Dim course As String
course = "ADCA"
If course = "ADCA" Or course = "Tally" Then
MsgBox "Course Available"
End If
If...Then can check a Boolean variable.
Dim isPaid As Boolean
isPaid = True
If isPaid = True Then
MsgBox "Fee Paid"
End If
A Boolean variable normally contains either True or False.
Dates can also be compared using If...Then.
Dim admissionDate As Date
admissionDate = Date
If admissionDate = Date Then
MsgBox "Admission Date is Today"
End If
The following example checks student marks and updates a worksheet.
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"
End If
This is a simple example of using InputBox, variables, cells, and If...Then together.
Beginners commonly make these mistakes:
Correct structure:
If marks >= 40 Then
MsgBox "Pass"
End If
If...Then can be used to check whether a required value has been entered.
Dim name As String
name = InputBox("Enter student name:")
If name = "" Then
MsgBox "Please enter a student name."
End If
This is a simple example of input validation.
Readable conditional code is easier to maintain and debug.
A typical If...Then workflow is:
Dim marks As Integer
marks = 70
If marks >= 40 Then
MsgBox "Pass"
End If
The If...Then statement allows VBA programs to make decisions based on conditions. It can work with variables, Excel cells, InputBox values, calculations, text, dates, and Boolean values.
Example:
Dim marks As Double
marks = CDbl(InputBox("Enter marks:"))
If marks >= 40 Then
Range("B2").Value = marks
Range("C2").Value = "Pass"
MsgBox "Student Passed"
End If
This basic decision-making concept is used extensively in practical VBA applications. In the next lesson, you will learn how to execute one block of code when a condition is true and another block when it is false using If...Then...Else.
Question: Which keyword is used after the condition in a VBA If statement?