CONDITIONAL ACTIONS WITH THE IF DECISION STRUCTURE

In this article I will be talking about a must decision/condition structure in macros. At the end of the article, we’ll have learned what IF Decision structure is and how and when to use it.

 

WHAT IS THE IF DECISION STRUCTURE?

This

condition structure is the structure that exists is all programming languages and allows the code blocks to run separately when certain conditions are met or not.

Allows you to execute actions depending on the True/False Boolean value that the indicated condition returns. If the condition is said to be True, the statements under the indicated condition(s) are applied. If the condition is said to be False, the statements indicated after the IF structure are applied. In addition, depending on the situation, if we have indicated the Else or ElseIF statements the control of those rows are done as well and when the outcome returns as True, the indicated commands are applied.

 

WHEN TO USE?

We use this structure when we have a thought/condition that starts with If or if we want to execute a multiple-condition action while coding.  There are a few other Decision/Condition structures other than this one; Select Case, IIF, Choose and Switch structures. Having knowledge of these structure will help you while coding.

 

HOW TO USE?

First, let’s take a look at its syntax, i.e. how we can write this IF structure on VBA.

 

SYNTAX

Here is its syntax.
Blue parts cannot be changed.
Red parts can be changed.

If condition(s) Then
‘Your codes
End If

You can think of its logic as “If this number is bigger than 10, do this…

If we have one condition only, you can use the syntax like this too:
If condition(s) Then ‘Your codes
If there is only one condition, you can remove the End If statement and write the condition next to the Then statement.
The conditions between If and Then statements can be multiple depending on the situation.
For example; If the numbers in the cells of the A column are bigger than 10 and not blank. You can use logical operators like And, Or, Xor.

Here is a little code sample for you to understand this part completely

If sayi > 10 And sayi < 20 Then

You can increase the condition to 2-3-4… by using logical operator in a If .. Then statement. Let’s say that we have some numbers in the A2:A10 call range and we want to write “bigger” on the B column of the numbers bigger 10.

Here is the code we need for this:

Sub IF_Kosul_Yapisi()
    For i = 2 To 10
        If Cells(i, "A") > 10 Then
            Cells(i, "B") = "Büyük"
        End If
    Next i
End Sub

You can see how these codes work in the GIF below.

IF structure

 

MULTIPLE CONDITIONS AND ELSE

If we have multiple conditions and you logically don’t want to execute actions depending on two situations like If it is like this do this, if not do that; you can write this in a line like this:

If Cells(i, "A") > 10 Then Cells(i, "B") = "Büyük" Else Cells(i, "B") = "Küçük"

and you can also write them one under the other with the Else statement. Then, your syntax will be like this:

If condition(s) Then
‘Your Codes If True
Else
‘Your Codes If False
End If

Some sample codes..

If Cells(i, "A") > 10 Then
    Cells(i, "B") = "Büyük"
        Else
    Cells(i, "B") = "Küçük"
End If

The code above writes Bigger to the B column of cells bigger than 10, and Smaller to the smaller ones.

 

ELSEIF

Now, let’s increase out conditions by including the ElseIf statement.
You can think the logic of this syntax as “If it’s like this to this; if not and it’s like that, do that; if not and it’s like the other way, then do that way.” 

Here is the syntax..
If conditions Then
‘your codes
ElseIf conditions Then
‘your codes
ElseIf conditions Then
‘your codes
End If

Let’s have our outcomes written to the Outcome column according to these conditions.

  • If the number is equal to zero, it will say Zero.
  • If the number is equal to 10, it will say Equal.
  • If the number is smaller than 10, it will say Smaller.
  • If the number is bigger than 10, it will say Bigger.

Depending on the conditions we’ve stated, the code we’ll need will be:

Sub Eger_Kosul_Yapısı_2()
    Dim i As Integer
    For i = 2 To Range("A65536").End(3).Row
        If Cells(i, "A") = 0 Then
            Cells(i, "B") = "Sıfır"
        ElseIf Cells(i, "A") = 10 Then
            Cells(i, "B") = "Eşit"
        ElseIf Cells(i, "A") < 10 Then
            Cells(i, "B") = "Küçük"
        ElseIf Cells(i, "A") > 10 Then
            Cells(i, "B") = "Büyük"
        End If
    Next i
End Sub

You can see how these codes work in the GIF below as well.

 

ELSEIF and ELSE TOGETHER

Lastly, if none of the conditions above are suitable for our codes, then we can include the Else statement before the End If row and get another outcome written in the cell as well.

Sub Eger_Kosul_Yapısı_3()
    Dim i As Integer
    For i = 2 To Range("A65536").End(3).Row
        If Cells(i, "A") = 0 Then
            Cells(i, "B") = "Sıfır"
        ElseIf Cells(i, "A") = 10 Then
            Cells(i, "B") = "Eşit"
        ElseIf Cells(i, "A") < 10 Then
            Cells(i, "B") = "Küçük"
        Else
            Cells(i, "B") = "Büyük"
        End If
    Next i
End Sub

 

You can take a look at the Condition Structures in Visual Basic for more details.

 

See you in other articles, bye. 🙋🏻‍♂️
You can share this article with your friends and get them informed as well. 👍🏻