0% found this document useful (0 votes)
2 views

vba if

The document explains the use of If statements in VBA for conditional checks, detailing the basic syntax and providing examples for simple If, If...Else, If...ElseIf, nested If statements, and the use of logical operators. It also covers comparison operators and their application in conditional statements. Key points emphasize the functionality of If statements in executing code based on specified conditions.

Uploaded by

abhishek sapra
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

vba if

The document explains the use of If statements in VBA for conditional checks, detailing the basic syntax and providing examples for simple If, If...Else, If...ElseIf, nested If statements, and the use of logical operators. It also covers comparison operators and their application in conditional statements. Key points emphasize the functionality of If statements in executing code based on specified conditions.

Uploaded by

abhishek sapra
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

In VBA, the If statement is used to perform conditional checks, meaning you can

execute certain blocks of code only if certain conditions are met.

Basic Syntax of If:


vba
Copy
If condition Then
' Code to execute if condition is True
End If
condition: The condition you want to check. If the condition is True, the code
inside the If block will run.

Example 1: Simple If Statement


Here’s an example where a simple condition checks if a number is greater than 10:

vba
Copy
Sub IfExample()
Dim number As Integer
number = 15

If number > 10 Then


Debug.Print "The number is greater than 10."
End If
End Sub
In this example, since number is 15, the message "The number is greater than 10."
will be printed in the Immediate Window.

Example 2: If...Else Statement


You can use an Else statement to execute code if the condition is False.

vba
Copy
Sub IfElseExample()
Dim number As Integer
number = 5

If number > 10 Then


Debug.Print "The number is greater than 10."
Else
Debug.Print "The number is not greater than 10."
End If
End Sub
Since number is 5 in this case, the output will be: "The number is not greater than
10."

Example 3: If...ElseIf...Else Statement


If you need to check multiple conditions, you can use ElseIf.

vba
Copy
Sub IfElseIfExample()
Dim number As Integer
number = 10

If number > 10 Then


Debug.Print "The number is greater than 10."
ElseIf number = 10 Then
Debug.Print "The number is exactly 10."
Else
Debug.Print "The number is less than 10."
End If
End Sub
In this case, since number is exactly 10, the output will be: "The number is
exactly 10."

Example 4: Nested If Statements


You can also nest If statements inside one another.

vba
Copy
Sub NestedIfExample()
Dim number As Integer
number = 8

If number > 5 Then


If number < 10 Then
Debug.Print "The number is between 5 and 10."
End If
End If
End Sub
In this case, since number is 8, the output will be: "The number is between 5 and
10."

Example 5: If with Logical Operators


You can also use logical operators such as And, Or, Not to combine multiple
conditions.

And:
vba
Copy
Sub IfAndExample()
Dim number As Integer
number = 7

If number > 5 And number < 10 Then


Debug.Print "The number is between 5 and 10."
End If
End Sub
Since the number is 7, which is between 5 and 10, the output will be: "The number
is between 5 and 10."

Or:
vba
Copy
Sub IfOrExample()
Dim number As Integer
number = 3

If number < 5 Or number > 10 Then


Debug.Print "The number is either less than 5 or greater than 10."
End If
End Sub
Since the number is 3 (which is less than 5), the output will be: "The number is
either less than 5 or greater than 10."

Not:
vba
Copy
Sub IfNotExample()
Dim isActive As Boolean
isActive = False

If Not isActive Then


Debug.Print "The status is not active."
End If
End Sub
Since isActive is False, the output will be: "The status is not active."

Key Points:
If checks if a condition is True, and if it is, it runs the code block inside.

You can use Else to specify code to run if the condition is False.

ElseIf allows you to check for multiple conditions.

Logical operators (And, Or, Not) can help you combine multiple conditions into one
If statement.

Example 6: Using If with Comparison Operators


Here are some common comparison operators:

=: Equal to

<>: Not equal to

>: Greater than

<: Less than

>=: Greater than or equal to

<=: Less than or equal to

vba
Copy
Sub ComparisonOperatorsExample()
Dim age As Integer
age = 20

If age >= 18 Then


Debug.Print "You are an adult."
Else
Debug.Print "You are a minor."
End If
End Sub
Since age is 20, the output will be: "You are an adult."

You might also like