How to Use If 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 use how to use the If statement in Excel VBA.
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 Statement
The syntax is :
If condition/expression Then Code Block for True value
Flow Diagram :

Example: Consider a company that wants to hire employees for a certain role. The company kept eligibility criteria as the age of the person. The age of the candidate who can apply for this interview must be less than 27 years.
Sub Allocate_Employee() 'Declaring and initializing the variable age Dim age As Integer age = 30 If age >= 27 Then MsgBox "You are not eligible for this post." End If End Sub
Output:
Since, the age is 30, the IF condition becomes TRUE and the code block inside IF statement executes.

Some helpful links to get more insights about Macros, VBA in Excel :


.png)