# Nested If Function Using VBA

Nested IF Function Macro
Last time we saw how we can use a simple IF-Function Macro to take care of two conditions. What if we wish to handle multiple conditions like grades based on marks ranges or discounts based on various quantities purchased or bonus based on a range of sales? Here we can use the nested-if function macros to automate the process of calculations or analysis.
The code is given below and it’s easy to understand. Instead of ‘else’ we use ‘elseif’ and for the last condition we use ‘else’. You can have upto 64 nested IFs in Excel 2007.

Sub how_to_use_nested_if()
Range(“D4”) = Range(“C4”) * Range(“B4”)
If Range(“B4”) >= 100 Then
Range(“E4”) = Range(“D4”) * 0.15
ElseIf Range(“B4”) >= 50 Then
Range(“E4”) = Range(“D4”) * 0.1
Else
Range(“E4”) = Range(“D4”) * 0.05
End If
Range(“F4”) = Range(“D4”) – Range(“E4”)
Range(“F4”) = Round(Range(“F4”), 2)
End Sub

## 2 thoughts on “Nested If Function Using VBA”

1. Amalendu says:

Dear Sir,
I am trying to run the following code, it showing the compile error Else without if can you kindly give me the solution Please?

Sub Condition()
Dim i
i = 3
Do While Cells(i, 1) “”
If Cells(i, 2) = “Formal” Then Cells(i, 3) = Cells(i, 2) * 0.1
Else: Cells(i, 3) = Cells(i, 2) * 100
End If
i = 1 + i
Loop
End Sub

2. Hrishikesh says:

Sir, your videos are very helpful.
I want a help from you for the following logic.
I have already put conditional formatting in the excel sheet.
Now I am getting the cells highlighted when the specific date reaches for a particular activity.
Now, I facing a problem that I must open this file to see which activity/activities have reached to alarm date and showing red, but I want to get the automatic pop up when I open my computer or I want the excel sheet open automatically in this case.
Kindly help.
Thanks and Regards
Hrishi

This site uses Akismet to reduce spam. Learn how your comment data is processed.