Lesson 31 of 60 – If...Then Statement in VBA
52%

If...Then Statement in VBA

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.

Note: The If...Then statement is one of the most important decision-making statements in VBA.

1. What is If...Then?

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.

2. Why Use If...Then?

If...Then is used when a program needs to make a decision.

For example:

  • Check whether a student passed.
  • Check whether marks are above a particular value.
  • Check whether a cell contains data.
  • Check whether a number is positive.
  • Check whether a fee is paid.

3. Basic Syntax of If...Then

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.

4. Simple If...Then Example

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.

5. Understanding the Condition

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.

6. Using Greater Than Operator

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

7. Using Less Than Operator

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

8. Using Equal To Operator

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

9. Using Not Equal Operator

The <> operator means not equal to.

Dim marks As Integer

marks = 55

If marks <> 40 Then
    MsgBox "Marks are not 40"
End If

10. If...Then with Excel Cells

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.

11. If...Then with Variables

Variables are commonly used with If...Then.

Dim salary As Double

salary = 25000

If salary > 20000 Then
    MsgBox "Salary is above 20000"
End If

12. If...Then with Text

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.

13. If...Then with InputBox

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

14. If...Then with MsgBox

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

15. If...Then for Student Result

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.

16. If...Then for Attendance

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

17. If...Then for Fee Payment

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

18. If...Then with Multiple Statements

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.

19. If...Then with Calculations

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

20. If...Then with a Range

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.

21. If...Then with Multiple Conditions

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.

22. If...Then with Or

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

23. If...Then with Boolean Values

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.

24. If...Then with Dates

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

25. Practical Student Example

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.

26. Common Mistakes with If...Then

Beginners commonly make these mistakes:

  • Forgetting Then.
  • Forgetting End If for a multi-line If block.
  • Using incorrect comparison operators.
  • Forgetting quotation marks around text.
  • Using the wrong variable data type.

Correct structure:

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

27. Practical Data Validation Example

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.

28. Best Practices for If...Then

  • Keep conditions simple and readable.
  • Use meaningful variable names.
  • Indent the code inside the If block.
  • Use clear comparison operators.
  • Use comments when the condition is complex.
  • Always check the result with different values.

Readable conditional code is easier to maintain and debug.

29. Complete If...Then Workflow

A typical If...Then workflow is:

  1. Declare a variable.
  2. Get or assign a value.
  3. Write a condition.
  4. Use the Then keyword.
  5. Write the statements to execute.
  6. Close the block with End If.
  7. Test the program with different values.
Dim marks As Integer

marks = 70

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

30. Complete Understanding of If...Then

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.

📌 Key Points

  • If...Then is used for decision making in VBA.
  • The condition is checked before the statements are executed.
  • The Then keyword follows the condition.
  • End If closes a multi-line If...Then block.
  • Comparison operators can be used to create conditions.
  • If...Then can work with Excel cells and variables.
  • If...Then can be combined with InputBox and MsgBox.
  • If the condition is false, the statements inside the block are skipped.

🧠 Quick Quiz

Question: Which keyword is used after the condition in a VBA If statement?