Professional Documents
Culture Documents
Lecture4 VBA PDF
Lecture4 VBA PDF
Decision
Prof. Carolina Peña Ortega, MsC
Universidad de la Salle
Outline
Wear a coat.
The If...Then Statement
The If...Then statement causes other statements to execute only
when an expression is true.
General Format
If expression Then
statement
more statements may follow)
End If
Display Message
Display Message
Nice weather we’re
A little cold, isn’t it?
having!
General Format
If expression Then
statement
(more statements may follow)
Else
statement
(more statements may follow)
End If
If the expression is True
o execute the statements between If…Then and Else
If the expression is False
o execute the statements between Else and End If
Two Mutually Exclusive Choices
True
Very cold? Wear a heavy jacket
False
True
Chilly? Wear a light jacket
False
True
Windy? Wear a windbreaker
False
True
Hot? Wear no jacket
False
General Format
If expression Then
statement
(more statements may follow)
ElseIf expression Then
statement
(more statements may follow)
(put as many ElseIf statements as necessary)
Else
statement
(more statements may follow)
End If
This construction is like a chain of If...Then...Else statements
The Else part of one statement is linked to the If part of another
Multiple Possible Choices
Sub prog04()
Dim temperature As Integer
temperature = InputBox("Write the temperature")
ActiveCell.Value = "The temperature is " & temperature
If Temperature < 40 Then
ActiveCell.Offset(1, 0).Value = "Wear a heavy jacket"
ElseIf Temperature <= 65 Then
ActiveCell.Offset(1, 0).Value = "Wear a light jacket"
ElseIf Temperature < 80 Then
ActiveCell.Offset(1, 0).Value = "Wear no jacket"
End If
End Sub
Example of ElseIf Usage (2)
The following table shows the approximate speed of sound in air, water,
and steel.
Medium Speed (feet per second)
Air 1100
Water 4900
Steel 16400
Write a program that displays a menu allowing the user to select air, water,
or steel. After the user has made a selection, he or she should be asked to
enter the distance a sound wave will travel in the selected medium. The
program will then display the amount of time it will take.
Input validation: Check that the user has selected one of the available
choices from the menu. Do not accept distances less than 0.
You must to use InputBox and MsgBox functions.
The Select Case Statement
In a Select Case statement, one of several possible actions is
taken, depending on the value of an expression.
The Select Case Statement
Similar to If…Then…ElseIf
o Performs a series of tests
o Conditionally executes the first true condition
Select Case is different in that:
o A single test expression may be evaluated
o The test expression is listed once
o The possible values of the expression are then listed with their
conditional statements
Case Else may be included and executed if none of the
values match the expression
Select Case General Format
Test Expression
Sub prog05()
Dim Day As Integer
Day = InputBox("Write 1 to Monday, 2 to Tuesday ... ")
Select Case Day
Case 1
MsgBox ("Day 1 is Monday.")
Case 2
MsgBox ("Day 2 is Tuesday.")
Case 3
MsgBox ("Day 3 is Wednesday.")
Case Else
MsgBox ("That value is invalid.")
End Select
End Sub
More about the Expression List:
Multiple Expressions
The Case statement’s expression list can contain multiple expressions, separated by commas
Sub prog06()
Dim Number As Integer, Status As String
Number = InputBox("Write numbers from 1 to 10 ")
Select Case intNumber
Case 1, 3, 5, 7, 9
Status = "Odd"
Case 2, 4, 6, 8, 10
Status = "Even"
Case Else
Status = "Out of Range"
End Select
ActiveCell.Value = Status
End Sub
More about the Expression List:
String Values
The Case statement can test string values
Sub prog07()
Dim Animal As String
Animal = InputBox("Write an animal ")
Select Case Animal
Case "Dogs", "Cats"
MsgBox ("House Pets")
Case "Cows", "Pigs", "Goats"
MsgBox ("Farm Animals")
Case "Lions", "Tigers", "Bears"
MsgBox ("Oh My!")
End Select
End Sub
More about the Expression List:
Relational Operators
You can use relational operators in the Case statement
The Is keyword represents the test expression in the relational comparison
Sub prog08()
Dim Temperature As Double
Temperature = InputBox("Write the temperature ")
Select Case Temperature
Case Is <= 75
ActiveCell.Value = "Too Cold!"
Case Is >= 100
ActiveCell.Value = "Too Hot!"
Case Else
ActiveCell.Value = "Just Right!"
End Select
End Sub
More about the Expression List:
Ranges of Values
Select Case Score
You can determine whether Case Is >= 90
the test expression falls within Grade = "A"
a range of values Case 80 To 89
The following table shows the approximate speed of sound in air, water,
and steel.
Medium Speed (feet per second)
Air 1100
Water 4900
Steel 16400
Write a program that displays a menu allowing the user to select air, water,
or steel. After the user has made a selection, he or she should be asked to
enter the distance a sound wave will travel in the selected medium. The
program will then display the amount of time it will take.
Input validation: Check that the user has selected one of the available
choices from the menu. Do not accept distances less than 0.
You must to use InputBox and MsgBox functions.
References
[1] Alexander, Michael; Kusleika, Dick. Excel 2016 Power Programming with
VBA. Reprint Edition. John Willey & Sons. 2017.
[2] Walkenbach, John. Excel 2013 Power Programming with VBA. John
Willey & Sons. 2013.
[3] Gaddis, Tony; Irvine, Kip R. Starting Out with Visual Basic. Student Value
Edition. Pearson. 2016.