If ELSE Statement codes example - VBA

If Then

VBA If Statements allow you to test if expressions are TRUE or FALSE, running different code based on the results.

Let’s look at a simple example:

If Range("a2").Value > 10 Then

Range("b2").Value = "Positive"



This tests if the value in Range A2 is greater than 0. If so, setting Range B2 equal to “Positive”

If condition

it will check value  in cell a2 if greater than 10 and enter the value positive in the cell b2

 ElseIF – Multiple Conditions

The ElseIf is added to an existing If statement. ElseIf tests if a condition is met ONLY if the previous conditions have not been met. and we want to put another condtion
 

Sub if_line()

If Range("a2").Value > 0 Then

    Range("b2").Value = "Positive"

ElseIf Range("a2").Value < 0 Then

    Range("b2").Value = "Negative"

End If

End Sub 



Elseif Condition 

Multiple elseif conditions

You can use multiple ElseIfs to test for multiple conditions, there is no limit to put elseif conditions:

Sub If_Multiple_Conditions()

    If Range("a2").Value = "Cat" Then

        Range("b2").Value = "Meow"

    ElseIf Range("a2").Value = "Dog" Then

        Range("b2").Value = "Woof"

    ElseIf Range("a2").Value = "Duck" Then

        Range("b2").Value = "Quack"

    End If

End Sub


Multiple If condition 

Else

The Else block will run if no other previous conditions have been met.

Sub If_Multiple_Conditions()

    If Range("a2").Value = "Cat" Then

        Range("b2").Value = "Meow"

    ElseIf Range("a2").Value = "Dog" Then

        Range("b2").Value = "Woof"

    ElseIf Range("a2").Value = "Duck" Then

        Range("b2").Value = "Quack"

    Else

         Range("b2").Value = "No Animal found"

    End If

End Sub

 

if with else


Nested IFs

You can also put if statements inside of another if statement.:

Sub Nested_Ifs()

    If Range("a2").Value > 0 Then

        Range("b2").Value = "Positive"

    Else

        If Range("a2").Value < 0 Then

            Range("b2").Value = "Negative"

        Else

            Range("b2").Value = "Zero"

        End If

    End If

End Sub


Nested If condition 

IF – Or, And, Xor, Not

If Or

The Or operator tests if at least one condition is met.

lets see with an example 

Sub If_or()

    If Range("a2").Value = "Dog" Or _

        Range("a2").Value = "Cat" Or _

        Range("a2").Value = "Cow" Then

       Range("b2").Value = "Animal"

End If

End Sub

 

If with OR

here if value in cell a2 is Dog or Cat or Cow, then it will change the value of b2 to animal

If And

The And operator allows you to test if ALL conditions are met.

let see with an example 

Sub if_and()

    If Range("a2").Value = "Dog" And _

        Range("a3").Value = "Cat" Then

       Range("b2", "b3").Value = "Animal"

    Else

       Range("b2", "b3").Value = "Not an Animal"

End If

End Sub

 

If with and 

here if the value of a2 is Dog and value of a3 is Cat only then it will change the value in b2 and b3 to Animal, if any of them is not met then it will change both to "Not an Animal"

If Xor

The Xor operator allows you to test if exactly one condition is met. If zero conditions are met Xor will return FALSE, If two or more conditions are met, Xor will also return false.

let see with an Example 

when all the condition are true

Sub if_XOR()

    If Range("a2").Value = "Dog" Xor _

        Range("a3").Value = "Cat" Then

       Range("b2", "b3").Value = "Animal"

    Else

       Range("b2", "b3").Value = "Not an Animal"

End If

End Sub


when all condition are true then else block gets executed, and the value of b2 and b3 changed to "Not and Animal"
 

when all the condition are true


when any one condition is true

Sub if_XOR()

    If Range("a2").Value = "Dog" Xor _

        Range("a3").Value = "Cat" Then

       Range("b2", "b3").Value = "Animal"

    Else

       Range("b2", "b3").Value = "Not an Animal"

End If

End Sub

when one condition is true then IF block gets executed, and the value of b2 and b3 changed to " Animal"

 

when any one condition is true

Post a Comment

0 Comments