The If Statement

In this blog post we discuss the If…Then…Else statements that will allow you to add conditional logic to your posts.

The Simple If Statement

Sub standardExpressions()

Dim a As Integer

a = 10
If a = 10 Then Debug.Print "a = 10"

'The If statement is a very simple statement that asks a
'straightforward question – is an expression True or False?

'If the expression is True then execute some code – in this case
'the “a=10” is printed in the immediate window.

'You can check this by changing the value of “a” to any other
'number and re-running the code (nothing will print out).

'The expression above uses what is termed an operator,
'the “=”equals operator.  To clearly show what the expression
'part of an If statement is, the above has been rewritten with
'braces around the expression in the box below.
a = 10
If (a = 10) Then Debug.Print "a = 10"

'Whatever is in brackets above, must equate to true for the
'code to run (Debug.Print "a = 10")

End Sub

The output to the immediate window will be:

a and b = 10
c=5 and a=5
a and b = 12

Comparing Multiple Expressions

Sub comparingMultipleExpressions()
'In this sub-procedure we show that the expression
'being evaluated can be complex and made up of more
'than one part.
Dim a As Integer
Dim b As Integer
Dim c As Integer

a = 10: b = 10
If (a = 10 And b = 10) Then Debug.Print "a and b = 10"

c = 5: a = 5
If (c = 5) And (a = 5) Then Debug.Print "c=5 and a=5"

a = 12: b = 12
If ((a = 12) And (b = a)) Then Debug.Print "a and b = 12"

'In the above code you can see that an expression doesn’t have to
'include actual values – numbers like 10 and 5 – but can consist
'of comparing variable against variable.
End Sub

The output to the immediate window will be:

a and b = 10
c=5 and a=5
a and b = 12

If…Then…Else

Sub ifThenElse()
'In this sub-procedure we take a look at the
'if...then...else statement.
Dim a As Integer
a = 12

If (a = 14) Then
    Debug.Print "a equals 14"
Else
    Debug.Print "a does not equal 14"
End If
'The above if statement essentially says:

'if a equals 14, print "a equals 14" to the immediate window
'HOWEVER if a does not equal 14, print "a does not equal 14"
'to the immediate window

'The 5 line syntax above is very common and should appear
'in your code often
End Sub

The output to the immediate window will be:

a does not equal 14

If…Then…ElseIf

Sub ifThenElseIf()
'In this sub-procedure we take a look at the
'if...then...elseif statement.

Dim a As Integer
a = 7

If (a = 5) Then
    Debug.Print "a equals 5"
ElseIf (a = 6) Then
    Debug.Print "a equals 6"
ElseIf (a = 7) Then
    Debug.Print "a equals 7"
End If

'The above if statement essentially says:

'if a equals 5, print "a equals 5" to the immediate window
'no, ok in that case:
'if a equals 6, print "a equals 6" to the immediate window
'no, ok in that case:
'if a equals 7, print "a equals 7" to the immediate window

End Sub

The output to the immediate window will be:

a equals 7

Combining Else with ElseIf

Sub combiningElseWithElseIf()
'In this sub-procedure we take a look at
'combining the elseif statement with the
'else statement

Dim a As Integer
a = 20

If (a = 15) Then
    Debug.Print "a equals 15"
ElseIf (a = 16) Then
    Debug.Print "a equals 16"
ElseIf (a = 17) Then
    Debug.Print "a equals 17"
Else
    Debug.Print "a does not equal 15, 16 or 17"
End If

'The above if statement essentially says:

'if a equals 15, print "a equals 15" to the immediate window
'no, ok in that case:
'if a equals 16, print "a equals 16" to the immediate window
'no, ok in that case:
'if a equals 17, print "a equals 17" to the immediate window
'no, well we have run out of options now so I will execute
'the Else statement and print:
'"a does not equal 15, 16 or 17" to the immediate window

End Sub

The output to the immediate window will be:

a does not equal 15, 16 or 17

Leave a Reply

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

Visit Us On TwitterVisit Us On FacebookVisit Us On Youtube