Return to VBA Code Examples

VBA Multiple (Nested) If Statements

This tutorial will show you how to use nested If statements  in VBA

If statements allow you to test for a single condition in VBA to see if the condition is True or False, and depending on the answer, the code will move in the direction of the true statement or the false statement.

A Single IF statement

Nested IFs explained

A Nested If allows you to put multiple conditions INSIDE each of the True and/or False statements of the original If.

Indenting your code when you write it always good practice as it makes the code easy to read and follow when you have to come back to it at some stage, or when another programmer has to read it.

We could also create a user designed function (UDF) and call the values of some cells from Excel into the function using parameters.

vba nested if

Nested If Practical Example

Consider the following function:

Using this function in an Excel sheet, we can test to see the total price for an order, and apply different discounts depending on that total.

vba nested ifs example

Using ElseIf

ElseIf enables us to simplify your code as it only moved down to the second if statement if the first one returns a false.

if else example

Using a Case Statement

We can also use a Case Statement to achieve the same effect.

vba if case example


VBA Coding Made Easy

Stop searching for VBA code online. Learn more about AutoMacro - A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users! vba save as

Learn More!