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
Range(“E4”) = Range(“D4”) * 0.05
End If
Range(“F4”) = Range(“D4”) – Range(“E4”)
Range(“F4”) = Round(Range(“F4”), 2)
End Sub

One thought on “Nested If Function Using VBA”

  1. 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
    End Sub

Leave a Reply

Your email address will not be published. Required fields are marked *