Return to VBA Code Examples

VBA Multiple (Nested) If Statements

This tutorial volition show yous how to use nested If statements  in VBA

If statements permit you lot to exam for a single condition in VBA to run across if the condition is Truthful or False, and depending on the respond, the code volition move in the direction of the true statement or the false statement.

A Single IF statement

Nested IFs explained

A Nested If allows you lot to put multiple weather condition Within each of the True and/or Fake statements of the original If.

Indenting your code when yous write it always good exercise as it makes the code piece of cake to read and follow when you accept to come back to it at some stage, or when another programmer has to read it.

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

vba nested if

Nested If Practical Example

Consider the following function:

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

vba nested ifs example

Using ElseIf

ElseIf enables u.s.a. to simplify your lawmaking as it only moved down to the 2d if statement if the first i returns a false.

if else example

Using a Example Argument

We tin can besides use a Case Argument to reach the same outcome.

vba if case example

VBA Coding Made Piece of cake

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

Larn More!