Tuesday, September 20, 2022

Working with If...Then...Else

If...Then...Else statement is to test certain condition either true or false and can be tested as many level as per required. Basically can be divide into 4 types:

Below example base on Table subject vs score, decide what to do base on score.

  1. Single condition with single line return (If...Then)

    No. Condition Return
    1 Score more than 50 Range background color to green

    VBA Code:

    Option Explicit
    Sub IFStatemantsSingleConditionSingleReturn()

        Dim i As Integer
        With ActiveSheet
            i = 2
            Do
                If .Range("C" & i) > 50 Then _
                Range("A" & i & ":C" & i).Interior.ColorIndex = 4
                i = i + 1
            Loop While .Range("A" & i) <> ""
        End With
        
    End Sub

  2. Single condition with multiple line return (If...Then...End If)

    No. Condition Return
    1 Score more than 50 Range background color to green
    Font text bold

    VBA Code:

    Option Explicit
    Sub IFStatemantsSingleConditionMultipleReturn()
        
        Dim i As Integer
        With ActiveSheet
            i = 2
            Do
                If .Range("C" & i) > 50 Then
                    Range("A" & i & ":C" & i).Interior.ColorIndex = 4
                    Range("A" & i & ":C" & i).Font.Bold = True
                End If
                i = i + 1
            Loop While .Range("A" & i) <> ""
        End With
        
    End Sub

  3. Double condition with multiple line return (If...Then...Else...End if)

    No. Condition Return
    1 Score more than 50 Range background color to green
    Font text bold
    2 Others Range background color to Yellow

    VBA Code:

    Option Explicit
    Sub IFStatemantsDoubleConditionMultipleReturn()
        
        Dim i As Integer
        With ActiveSheet
            i = 2
            Do
                If .Range("C" & i) > 50 Then
                    Range("A" & i & ":C" & i).Interior.ColorIndex = 4
                    Range("A" & i & ":C" & i).Font.Bold = True
                Else
                    Range("A" & i & ":C" & i).Interior.ColorIndex = 6
                End If
                i = i + 1
            Loop While .Range("A" & i) <> ""
        End With
        
    End Sub

  4. Multiple condition with multiple line return (If...Then...Elseif...Then...Else...End if)

    No. Condition Return
    1 Score more than 80 Range background color to green
    Font text bold
    2 Score more than 50 Range background color to Yellow
    3 Others Range background color to Red

    VBA Code:

    Option Explicit
    Sub IFStatemantsMultipleConditionMultipleReturn()
        
        Dim i As Integer
        With ActiveSheet
            i = 2
            Do
                If .Range("C" & i) > 80 Then
                    Range("A" & i & ":C" & i).Interior.ColorIndex = 4
                    Range("A" & i & ":C" & i).Font.Bold = True
                ElseIf .Range("C" & i) > 50 Then
                    Range("A" & i & ":C" & i).Interior.ColorIndex = 6
                Else
                    Range("A" & i & ":C" & i).Interior.ColorIndex = 3
                End If
                i = i + 1
            Loop While .Range("A" & i) <> ""
        End With
        
    End Sub

Note:
Normally select case statement is more readable instead or using If...Then...Else statement with multiple condition and If...Then...Else statement can be place inside another if statement but be careful to close with End if properly.

Microsoft Reference - If..Then..Else statement

Practice makes perfect. Thank You.

No comments:

Post a Comment