How to use If-Else-If Statement in Excel VBA?
Last Updated :
19 Nov, 2021
In this article, we are going to look into how to use the If Else If statement in Excel VBA using a suitable example.
Implementation :
In the Microsoft Excel tabs, select the Developer Tab. Initially, the Developer Tab may not be available.
The Developer Tab can be enabled easily by a two-step process :
- Right-click on any of the existing tabs at the top of the Excel window.
- Now select Customize the Ribbon from the pop-down menu.

- In the Excel Options Box, check the box Developer to enable it and click on OK.

- Now, the Developer Tab is visible.
Now click on the Visual Basic option in the Developer tab and make a new module to write the program using the Select Case statement.
Developer -> Visual Basic -> Tools -> Macros
- Now create a Macro and give any suitable name.

- This will open the Editor window where can write the code.

IF ELSE IF statement
The syntax is :
If condition1/expression1 Then
Code Block 1
Else If condition2/expression2 Then
Code Block 2
Else
Code Block 3
End If
In this initially, the If condition is executed and if it is TRUE then the code block 1 will execute and the program terminates. Now, if condition 1 becomes FALSE then condition 2 inside Else IF will work, and if it is TRUE code block 2 will execute and the program terminates. If both the previous conditions are false then Else will work and Code Block 3 will be executed.
Flow Diagram :
Example : Suppose in a company we need to find the maximum salary of three departments : HR, Finance and IT.
Code :
Sub Find_Max()
'Declaring the variables
Dim HR_Sal As Integer
Dim Fin_Sal As Integer
Dim IT_Sal As Integer
'Asking the users to enter the salary
HR_Sal = InputBox("Enter HR department salary:")
Fin_Sal = InputBox("Enter Finance department salary:")
IT_Sal = InputBox("Enter IT department salary:")
'Conditions to find the maximum salary of three departments
If HR_Sal > Fin_Sal And HR_Sal > IT_Sal Then
MsgBox "HR department has maximum salary"
'The previous condition fails then the below statement executes
ElseIf Fin_Sal > IT_Sal Then
MsgBox "Finance department has maximum salary"
'The previous two conditions fail then the below statement executes
Else
MsgBox "IT department has maximum salary"
End If
End Sub
Here, first we check if HR Salary is more than that of the other two departments. If it is TRUE then definitely the HR department has maximum salary and the code inside IF executes. If this is not TRUE, it means HR department salary is not maximum and next in Else If condition we need to compare between IT and Finance and see which is maximum. In this way If Else If executes. If none of the previous conditions are TRUE, then the code inside Else will be executed.
AND is a logical operator which gives the result as TRUE value if and only if both the expressions associated with this operator are TRUE.
Case 1 :
Inputs by the user are :


The output is :
Case 2 :
Inputs by the user are :


The output is :
Case 3 :
Inputs by the user are :


The output is :
Similar Reads
How to use If-Else Statement in Excel VBA? VBA in Excel stands for Visual Basic for Applications which is Microsoft's programming language. To optimize the performance and reduce the time in Excel we need Macros and VBA is the tool used in the backend. In this article, we are going to learn how to use the If Else statement in Excel VBA. Impl
3 min read
How to Use Select Case Statement in Excel VBA? VBA in Excel stands for Visual Basic for Applications which is Microsoft's programming language. To optimize the performance and reduce the time in Excel we need Macros and VBA is the tool used in the backend. In this article, we are going to discuss how to use Select Case Statement in Excel VBA. Se
10 min read
How to Use For Next Loop in Excel VBA? If you are familiar with the programming you must have an idea of what a loop is, In computer programming, a loop is a sequence of statements that are repeated until a specific condition is satisfied. In Excel VBA the "For Next" loop is used to go through a block of code a specific number of times.
4 min read
IF-ELSE-IF statement in R if-else-if ladder in R Programming Language is used to perform decision making. This ladder is used to raise multiple conditions to evaluate the expressions and take an output based on it. This can be used to evaluate expressions based on single or multiple conditions connected by comparison or arit
2 min read
How to Use the VBA Immediate Window in Excel? The immediate window is similar to the console in Chrome, where we could execute the javascript. The immediate window allows us to execute macro code very fast and efficiently. Once, you learned it, you will always be found using it. Your immediate window can answer an infinite number of questions,
5 min read
How to Delete a Module in Excel VBA? Have you ever seen a text file in a notepad or a source code file in visual studio code? These all are just basic files where you can write some code or information. Similarly, we have modules in excel, each module has its code window, where you can write some code and allow it to automate your repe
3 min read
VBA Multiple (Nested) If Statements in Excel VBA in Excel stands for Visual Basic for Applications, which is Microsoft's programming language. To optimize the performance and reduce the time in Excel we need Macros and VBA is the tool used in the backend. What decision-making reasoning do you often use in your Excel worksheets? In most circums
5 min read
How to Use for Each Loop in Excel VBA? A For Each loop is used to execute a statement or a set of statements for each element in an array or collection. Syntax: For Each element In group [ statements ] [ Exit For ] [ statements ] Next [ element ] The For...Each...Next statement syntax has the following three parts: PartDescriptionelement
3 min read
How to Set Variable to Cell Value in Excel VBA? Adding data to the worksheet is often done and it is essential to learn how to set variable to cell value in excel VBA. There can be instances when you want to add the same data to your worksheet. This task could easily be achieved with the help of Excel VBA variables. You can assign your cell value
5 min read
How to Insert and Run VBA Code in Excel? In Excel VBA stands for (Visual Basic for Application Code) where we can automate our task with help of codes and codes that will manipulate(like inserting, creating, or deleting a row, column, or graph) the data in a worksheet or workbook. With the help of VBA, we can also automate the task in exce
2 min read