Conditional
Statements
1
If Statement
• VBA uses Boolean expressions, along with If
statements, to control the flow of execution of a
program
If condition Then ‘here condition is a Boolean expression
action1
Else
action2
End If
2
If Statement Flowchart
Condition true?
yes no
Perform action 1 Perform action 2
Bringing the branches back together
gives the flowchart a better structure
3
The arrows are one-way streets.
Only one action can (and must)
be performed.
If Statement Example
If varA > varB Then
max = varA
min = varB
Else
max = varB
min = varA
End If
• Note: In real life we would use the Max and Min
functions but we are keeping things simple to start
with 4
If Statement Example
‘ compute shipping charge, with a discount for more expensive orders
If price >= discountShippingPrice Then
shippingCharge = price * discountShippingChargeRate
Else
shippingCharge = price * regularShippingChargeRate
End If
totalCharge = price + shippingCharge
5
If Statement Options
• The Else part can be omitted:
If condition Then
action1
End If
• There can be multiple ElseIf branches
If condition1 Then ‘if condition1 is True do action1, end
action1
ElseIf condition2 Then ‘if condition1 is F & condtion2 is T, do action2
action2
Else ‘if both conditions are F, do action3
action3
EndIf
6
Single Branch If Statement Example
‘ compute shipping charge if applicable
shippingCharge = 0
If price < freeShippingPrice Then
shippingCharge = price * shippingChargeRate
End If
price = price + shippingCharge
‘ if the If statement interior is not executed, then shippingCharge
is 0
7
Single Branch If Statement Flowchart
Condition true?
yes no
Perform action
Bringing the branches back together
gives the flowchart a better structure
8
Multiple Branch If Statement Example
‘ Set thank you message based on tip size
tipPercent = (tipAmount/baseCharge) * 100
If tipPercent < 15 Then
txtThankYou.Text = “Thanks.”
ElseIf tipPercent < 20 Then
txtThankYou.Text = “Thank you and have a nice day.”
ElseIf tipPercent < 25 Then
txtThankYou.Text = “Thank you very much! Have a nice day.”
Else ‘we know the tip is at least 25%
txtThankYou.Text = “Thank you!! Have a GREAT Day!”
End If
9
Multiple Branch If Statement Flowchart
Condition 1 true?
yes
no
Perform action 1
10
no
no
yes
yes
Perform action 2
Perform action 3
Perform else action
Condition 2 true?
Condition 3 true?
The else is
optional. The
arrows are one-
way streets
Nesting: If’s inside of If’s
• You can nest entire If statements inside the If
or Else part of another If statement
• Nesting more than one or two deep is strongly
discouraged! It makes the program hard to
read and understand
• Try to use Elseif or more complex conditions
instead
11
Nested If’s Example
‘ Select title based on language and gender
If language = “French” Then
If gender = “Female” Then
title = “Mademoiselle”
Else
title = “Monsieur”
Endif
ElseIf language = “English” Then
If gender = “Female” Then
title = “Miss”
Else
title = “Mister”
EndIf
Else
title = “” ‘no title in this case
EndIf
12
Converting to ElseIfs
‘ Select title based on language and gender
If language = “French” And gender = “Female” Then
title = “Mademoiselle”
ElseIf language = “French” And gender = “Male” Then
title = “Monsieur”
ElseIf language = “English” And gender = “Female” Then
title = “Miss”
ElseIf language = “English” And gender = “Male” Then
title = “Mister”
Else
title = “” ‘ it’s usually best to have an else case
EndIf
13
Select Case Statements
• The Select Case Statement can be used when
there are multiple options to choose from
• It can simplify program structure
• It makes the logical structure of the program
clear when a nested if or long Else If structure
might not
14
Case Statement Example
Assume position is a variable with a value between 1 and some
number from 2 to 50
Select Case postion
Case 1
txtOutcome.Text = “Win” ‘several lines could go here
Case 2
txtOutcome.Text = “Place”
Case 3
txtOutcome.Text = “Show”
Case 4,5
txtOutcome.Text = “Close but no cigar”
Case Else
txtOutcome.Text = “Out of the money”
End Select
15
Conditionals Overview
We’ve looked at program elements that let us
write conditions and branches in programs:
– Boolean constants True and False
– Comparison operators to form Boolean expressions
– Boolean operators to build more complex expressions;
truth tables to check them
– If statements, three types (with Else, with no Else,
with ElseIfs)
– Nested If’s
– Select Case statements

Conditional statements

  • 1.
  • 2.
    If Statement • VBAuses Boolean expressions, along with If statements, to control the flow of execution of a program If condition Then ‘here condition is a Boolean expression action1 Else action2 End If 2
  • 3.
    If Statement Flowchart Conditiontrue? yes no Perform action 1 Perform action 2 Bringing the branches back together gives the flowchart a better structure 3 The arrows are one-way streets. Only one action can (and must) be performed.
  • 4.
    If Statement Example IfvarA > varB Then max = varA min = varB Else max = varB min = varA End If • Note: In real life we would use the Max and Min functions but we are keeping things simple to start with 4
  • 5.
    If Statement Example ‘compute shipping charge, with a discount for more expensive orders If price >= discountShippingPrice Then shippingCharge = price * discountShippingChargeRate Else shippingCharge = price * regularShippingChargeRate End If totalCharge = price + shippingCharge 5
  • 6.
    If Statement Options •The Else part can be omitted: If condition Then action1 End If • There can be multiple ElseIf branches If condition1 Then ‘if condition1 is True do action1, end action1 ElseIf condition2 Then ‘if condition1 is F & condtion2 is T, do action2 action2 Else ‘if both conditions are F, do action3 action3 EndIf 6
  • 7.
    Single Branch IfStatement Example ‘ compute shipping charge if applicable shippingCharge = 0 If price < freeShippingPrice Then shippingCharge = price * shippingChargeRate End If price = price + shippingCharge ‘ if the If statement interior is not executed, then shippingCharge is 0 7
  • 8.
    Single Branch IfStatement Flowchart Condition true? yes no Perform action Bringing the branches back together gives the flowchart a better structure 8
  • 9.
    Multiple Branch IfStatement Example ‘ Set thank you message based on tip size tipPercent = (tipAmount/baseCharge) * 100 If tipPercent < 15 Then txtThankYou.Text = “Thanks.” ElseIf tipPercent < 20 Then txtThankYou.Text = “Thank you and have a nice day.” ElseIf tipPercent < 25 Then txtThankYou.Text = “Thank you very much! Have a nice day.” Else ‘we know the tip is at least 25% txtThankYou.Text = “Thank you!! Have a GREAT Day!” End If 9
  • 10.
    Multiple Branch IfStatement Flowchart Condition 1 true? yes no Perform action 1 10 no no yes yes Perform action 2 Perform action 3 Perform else action Condition 2 true? Condition 3 true? The else is optional. The arrows are one- way streets
  • 11.
    Nesting: If’s insideof If’s • You can nest entire If statements inside the If or Else part of another If statement • Nesting more than one or two deep is strongly discouraged! It makes the program hard to read and understand • Try to use Elseif or more complex conditions instead 11
  • 12.
    Nested If’s Example ‘Select title based on language and gender If language = “French” Then If gender = “Female” Then title = “Mademoiselle” Else title = “Monsieur” Endif ElseIf language = “English” Then If gender = “Female” Then title = “Miss” Else title = “Mister” EndIf Else title = “” ‘no title in this case EndIf 12
  • 13.
    Converting to ElseIfs ‘Select title based on language and gender If language = “French” And gender = “Female” Then title = “Mademoiselle” ElseIf language = “French” And gender = “Male” Then title = “Monsieur” ElseIf language = “English” And gender = “Female” Then title = “Miss” ElseIf language = “English” And gender = “Male” Then title = “Mister” Else title = “” ‘ it’s usually best to have an else case EndIf 13
  • 14.
    Select Case Statements •The Select Case Statement can be used when there are multiple options to choose from • It can simplify program structure • It makes the logical structure of the program clear when a nested if or long Else If structure might not 14
  • 15.
    Case Statement Example Assumeposition is a variable with a value between 1 and some number from 2 to 50 Select Case postion Case 1 txtOutcome.Text = “Win” ‘several lines could go here Case 2 txtOutcome.Text = “Place” Case 3 txtOutcome.Text = “Show” Case 4,5 txtOutcome.Text = “Close but no cigar” Case Else txtOutcome.Text = “Out of the money” End Select 15
  • 16.
    Conditionals Overview We’ve lookedat program elements that let us write conditions and branches in programs: – Boolean constants True and False – Comparison operators to form Boolean expressions – Boolean operators to build more complex expressions; truth tables to check them – If statements, three types (with Else, with no Else, with ElseIfs) – Nested If’s – Select Case statements