If Then, If Then Else and If Else If statement makes the programmers control the execution flow of a script or one of its sections.
Below image is the general form of structure found in most programming languages.
If Then Statement
The If-then statement is the simplest of all control flow statements; it tells your program to execute only a selected portion of code if a certain test checks out as true.
It also contains a Boolean expression that allows one or more statements.
This also tells if the condition is True that the statement under if condition(s) are executed. i.e if the condition is False, the statements after the If loop are executed.
Syntax
If (Boolean_expression) Then Statement 1 ... Statement n End If
To run one statement when a condition is True, you need to use single line syntax of the If statement.
For Example
Sub VBAIfElse_Example1() Dim x As Integer x = 1 If x Then x = True End Sub
For you to run more than one line of code, then you can use the multiple-line syntax. This syntax includes the End If statement.
For Example
Sub VBAIfElse_Example2() Dim x As Integer x = 1 If x Then ActiveCell.Interior.Color = 255 ActiveCell.Font.Bold = True ActiveCell.Font.Italic = True End If End Sub
Flow Diagram
Example
Let’s take a look and find the largest between the two numbers of Excel with the help of a function.
After have executed the above code, you will get the output below
Sub VBAIfElse_Example3() Dim x As Integer, y As Integer x = 50 y = 20 If x > y Then MsgBox "x is greater than y" End If End Sub
x is greater than y
Here are some more examples of If Then statements: This VBA code checks whether the specified condition equals True or False.
The result of your code will be displayed as a MsgBox in the event that the condition is True. If the condition evaluates to False, then the VBA code does nothing, and the line is skipped immediately.
See More: Nested If Statement
For Example
Sub VBAIfElse_Example4() Dim x As Integer x = 10 If x Mod 2 = 0 Then MsgBox "x is even" End If End Sub
In the above example, if the number contained in the x of excel has the remainder zero when divided by 2, then excel displays a MsgBox that tells the number is even.
If Then Else Statement
The if-then-else statement provides a secondary path of execution when an “if” clause evaluates to false.
An If statement contains a Boolean expression that allows one or more statements, If the condition is True, the statements under the If condition are executed, whereas if the condition is false, the statements under the Else condition are executed.
Syntax
If (Boolean_expression) Then Statement 1 ... Statement n Else Statement 1 … Statement n End If
Based on the syntax analysis, you can perform different tasks according to the result of the condition.
For Example
Sub VBAIfElse_Example5() Dim x As Integer x = 10 If x Mod 2 = 0 Then MsgBox "x is even" Else MsgBox "x is odd" End If End Sub
Now when you run your VBA code, the output will be the same regardless of what x numeric value is. The output is illustrated in the screenshot below.
If Then Else is a built-in Excel function that is classified as a logical function and works by sending one set of code to be executed if a specified condition evaluates true and another set of code evaluates false. It can also be used as a VBA function in Excel; it can be used in macros created with the Microsoft Visual Basic Editor.
Flow Diagram
Example
Let’s quickly find the largest between the two numbers of Excel with the help of a function.
Sub VBAIfElse_Example6() Dim x As Integer, y As Integer x = 50 y = 20 If x > y Then MsgBox "x is greater than y" Else MsgBox "y is greater than x" End If End Sub
After you’ve executed the above code, you will get the following output:
x is greater than y
Let’s try and use the following examples to understand the ElseIf statements more better.
If Elself Statement
elseif, as its name suggests, is a combination of if and else. Like else, it extends an if statement to execute a different statement in case the original if the expression evaluates to false. However, unlike else, it will execute that alternative expression only if the elseif conditional expression evaluates to true.
Syntax
If (Boolean_expression) Then Statement 1 ... Statement n ElseIf (Boolean_expression) Then Statement 1 ... Statement n ElseIf (Boolean_expression) Then Statement 1 ... Statement n Else Statement 1 ... Statement n End If
Not every condition can be reduced to a simple two statement. You also have more than two options in your code. In this situation, you can use the ElseIf statement.
You can use more than one or two ElseIf parts, as many as you need as seen in the above syntax.
Flow Diagram
Example
Let’s find the largest between the two numbers of Excel with the help of a function.
Sub VBAIfElse_Example7() Dim x As Integer, y As Integer x = 20 y = 20 If x > y Then MsgBox "x is greater than y" ElseIf y > x Then MsgBox "y is greater than x" Else MsgBox "x is equal to y" End If End Sub
After you have executed the above code you will get the output such as:
x and y are Equal