VBA Multiple IFS

Different languages have different syntaxes for nested if statements. In this lesson, I’m going to show you, how you can use multiple if statements in VBA in a few different examples.

Nested If statements

The first example I’m going to show you two ways you can use multiple ifs. The first way will work, but it’s not the elegant one.

Here, you have a lot of code and when you have to close the IF condition this is not going to be very readable.

If and ElseIf

That’s why there is an ElseIf condition. It is executed only if the previous one returned FALSE. Take a look at the modified example.

This time the code is much more readable than the last one. In this example, the first three conditions are not met, so Excel displays the last message.

Select … Case statement

If you have multiple cases to choose from, you can also use the Select … Case statement. It works in a similar way to If … Else.

Nesting If and Select … Case statements

You can also combine If and Select … Case statements. This example shows a practical example of how you can use it in real life.

Let’s say you buy bicycles, motorcycles, and cars. The discount depends on the type and the number of vehicles you buy. We want to buy 7 motorcycles. Run the code.

After you run the code, Excel will display this message.

Tomasz Decker is an Excel specialist, skilled in data analysis and financial modeling.