3. USING LOGIC
Run specific code based on what the user does
E.g., if they select a specific stock, then only show info
about that stock.
We need to use LOGIC
There are two ways to do this:
Select Case
If Then statements
5. LECTURE 7 STUDENT EXAMLE.XLSM
The userform for each of the following 2 examples
are in the above excel file on blackboard.
Download this file and follow along with the
examples.
7. EXAMPLE 1. SELECT CASE
ComboBox1
Case Is = “ItemONE”
Is
ComboBox1.Value = “ItemONE”
8. EXAMPLE 1. SELECT CASE
ComboBox1
Case Is = “ItemONE”
Is
ComboBox1.Value = “ItemONE”
Yes
• Show this
MsgBox
• End Select
No
Go to the
next Case Is
9. EXAMPLE 1. SELECT CASE
ComboBox1
Case Is = “ItemONE”
Case Is = “ItemTWO”
Does
ComboBox1.Value = “ItemTWO”
10. EXAMPLE 1. SELECT CASE
ComboBox1
Case Is = “ItemONE”
Case Is = “ItemTWO”
Does
ComboBox1.Value = “ItemTWO”
Yes
• Show this
MsgBox
• End Select
No
Go to
Case Else
11. EXAMPLE 1. SELECT CASE
ComboBox1
Case Is = “ItemONE”
Case Is = “ItemTWO”
None of the Case Is
statements are true
Yes
Show this MsgBox
Exit the Sub procedure
12. EXAMPLE 1. SELECT CASE
ComboBox1
Case Is = “ItemONE”
Case Is = “ItemTWO”
Give two examples
of when Case Else
would be true
13. EXAMPLE 1. SELECT CASE
ComboBox1
Case Else
Case Else
Give two examples
of when Case Else
would be true
15. EXAMPLE 2. SELECT CASE
How can we prevent
errors from non-numeric
entries?
16. EXAMPLE 2. SELECT CASE
We test for correct
entries rather than
incorrect entries
17. EXAMPLE 2. SELECT CASE
Just ‘Case’ not ‘Case Is’
for a range like this.
18. EXAMPLE 2. SELECT CASE
Case Else takes care of
every value not
between 0-100.
19. SELECT CASE. GENERAL FORM.
Select Case Name what is being tested
Case Is = Condition 1
…
Case Is = Condition 2
…
Case Else
…
End Select
For example:
• Name of control
• Name of variable
• Name of range
The words in BOLD are VBA key words, so don’t
change them.
20. SELECT CASE. GENERAL FORM.
Select Case Name what is being tested
Case Is = Condition 1
…
Case Is = Condition 2
…
Case Else
…
End Select
You can have as many
conditions as you need
The words in BOLD are VBA key words, so don’t
change them.
21. SELECT CASE
Select Case Name what is being tested
Case Is = Condition 1
…
Case Is = Condition 2
…
Case Else
An operator:
…
=, >, <, <>
End Select
The words in BOLD are VBA key words, so don’t
change them.
22. SELECT CASE
Select Case Name what is being tested
Case Is = Condition 1
…
Case Is = Condition 2
…
Case Else
…
End Select
Omit if Condition is a
range (like example 2).
The words in BOLD are VBA key words, so don’t
change them.
24. IF – THEN STATEMENTS
Logic test – Tests IF something is True.
IF A is True, THEN do B
If
Then
A
B
End If
Can be multiple
lines of code
If B is only one line of code, then you can write:
If
A
Then
B
25. EXAMPLE. IF – THEN.
IF A is True, THEN do B
If x < 25 Then
y = x + 17
End If
OR
If x < 25 Then y = x + 17
What happens when 𝑥 ≥ 25?
26. EXAMPLE. IF – THEN.
IF A is True, THEN do B
If x < 25 Then
y = x + 17
MsgBox (“y = ” & y)
End If
Multiple lines of code so we
MUST use this syntax
Cannot use one-line syntax
27. EXERCISE #1. LOGIC.
Download Lecture 7 Student Example.xlsm.
Open UserForm1
Code the Next button to test if the value of
TextBox1 is <0 (they should enter a value >0).
If it is less than 0, display an error message telling the user
what they did wrong. Then exit the sub.
If
Then
A
B
End If
28. IF – THEN – ELSE STATEMENTS
Logic tests – Tests IF something is True.
IF A is True, THEN do B
If
Then
A
If
B
End If
Then
A
B
Else
C
End If
IF A is True, THEN do B, ELSE do C
29. IF – THEN-ELSE STATEMENTS
IF A is True, THEN do B, ELSE do C
If
Then
A
B
Can be multiple
lines of code
Else
C
End If
If B and C are only one line of code, you can write:
If
A
Then
B
Else
C
30. EXAMPLE. IF – THEN – ELSE.
IF A is True, THEN do B, ELSE do C
If x < 25 Then
y = x + 17
Else
y= x-4
End If
When will
y = x – 4?
OR
If x < 25 Then y = x + 17 Else y = x - 4
31. EXERCISE #2. IF – THEN – ELSE.
Use UserForm1 again
Re-write your If-Then statement to test if the
textbox is >= 0.
If it is then:
Assign the value of the textbox to a variable called numStocks
Output a message stating the number of stocks entered by the user
If it isn’t then:
Give an error message and exit the sub.
If
Then
A
B
Else
C
End If
We are assuming that the
user will only enter a
number and not any other
non-numeric value
causing an error.
32. EXAMPLE. IF – THEN – ELSE.
If x < 25 Then
y = x + 17
Else
If x > 100 Then
y= x-4
End If
End If
When will
y = x – 4?
33. EXAMPLE. IF – THEN – ELSE.
If x < 25 Then
y = x + 17
Else
If x > 100 Then
y= x-4
End If
End If
If x < 25 Then
y = x + 17
ElseIf x > 100 Then
y= x-4
End If
34. IF – THEN-ELSEIF STATEMENTS
If
Then
A
If
Then
A
B
B
Elseif
C
Then
Elseif
C
D
D
Else
End If
Optional
E
End If
IF A is True, THEN do B
IF C is True, THEN do D
IF A and C are False, THEN do E
Then
35. EXERCISE #3. IF – THEN – ELSE.
Open Module1 and Exercise3()
Re-write the two If-Then statements so that you
have one If-Then-Elseif statement
If x < 75 Then
y = 5*x
End If
If x > 200 Then
y = 2*x
End If
36. IF-THEN AND SELECT CASE
Both are logic statements
If you are only testing one condition, then use an
if-then statement.
If you have several conditions, it’s best to use
Select Case.
If x < 75 Then
y = 5*x
Elseif x > 200 Then
y = 2*x
End If
Select Case x
Case Is < 75
y = 5*x
Case Is > 200
y = 2*x
End Select
37. IF-THEN AND SELECT CASE
If x < 75 Then
y = 5*x
Elseif x > 200 Then
y = 2*x
Else
y=x
End If
Select Case x
Case Is < 75
y = 5*x
Case Is > 200
y = 2*x
Case Else
y= x
End Select
39. USING LOGICAL OPERATORS – OR
If x < 25 Then
y= x+1
End If
If x > 60 Then
y= x+1
End If
If x < 25 OR x > 60 Then
y= x+1
End If
40. USING LOGICAL OPERATORS – OR
If x < 25 Then
y= x+1
End If
If x > 60 Then
y= x+1
End If
If x < 25 OR x > 60 Then
y= x+1
End If
What if
x=5?
41. USING LOGICAL OPERATORS – OR
If x < 25 Then
y= x+1
End If
If x > 60 Then
y= x+1
End If
If x < 25 OR x > 60 Then
y= x+1
End If
What if
x=35?
42. USING LOGICAL OPERATORS – OR
If x < 25 Then
y= x+1
End If
If x > 60 Then
y= x+1
End If
If x < 25 OR x > 60 Then
y= x+1
End If
What if
x=100?
43. USING LOGICAL OPERATORS – AND
If x < 25 Then
If x > 0 Then
y= x+5
End If
End If
Both must be true
If x > 0 AND x < 25 Then
y= x+5
End If
44. EXERCISE #4. IF – THEN – ELSE.
Use UserForm1 again.
Amend your If-Then statement to test if the value
of the textbox is between 0 and 100:
If it is then:
Assign the value of the textbox to a variable called numStocks
Output a message stating the number of stocks entered by the user
If it isn’t then:
Give an error message and exit the sub.
We are assuming that the
user will only enter a
number and not any other
non-numeric value
causing an error.
45. EXERCISE #5. USING LOGICAL OPERATORS
Open Module1 and Exercise5()
Rewrite the following.
Decide if you should use AND or OR
47. WHAT ‘ERRORS’ SHOULD I CHECK FOR?
Did they enter a number?
If not, give an error message and exit the sub
If yes, then continue
Did they enter a number ≥ 0 and ≤ 100?
If not, give an error message and exit the sub
If yes, then continue
We did this already
in Exercise #4
48. WHAT ‘ERRORS’ SHOULD I CHECK FOR?
Did they enter an integer number?
If not, give an error message and exit the sub
If yes, then continue
49. CHECKING FOR ERRORS USING LOGIC
We don’t have to test for everything separately.
We can be more clever and use ‘Else’ to capture
most errors.
But, we will need to test if the number is an
integer using an if-statement.
We will do this first, then account for the other possible
errors.
51. IS THE NUMBER AN INTEGER?
Dim numStocks as Integer
No matter what value is
assigned to numStocks, it will
be rounded to an integer value
52. IS THE NUMBER AN INTEGER?
Dim numStocks as Integer
numStocks = TextBox1.Value
What is the value of numStocks?
No matter what value is assigned to numStocks,
it will be rounded to an integer value
53. IS THE NUMBER AN INTEGER?
Dim numStocks as Integer
numStocks = TextBox1.Value
What is the value of numStocks?
numStocks = 8
No matter what value is assigned to numStocks,
it will be rounded to an integer value
54. IS THE NUMBER AN INTEGER?
Dim numStocks as Integer
numStocks = TextBox1.Value
What is the value of numStocks?
No matter what value is assigned to numStocks,
it will be rounded to an integer value
55. IS THE NUMBER AN INTEGER?
Dim numStocks as Integer
numStocks = TextBox1.Value
What is the value of numStocks?
numStocks = 10
No matter what value is assigned to numStocks,
it will be rounded to an integer value
56. IS THE NUMBER AN INTEGER?
Dim numStocks as Integer
numStocks = TextBox1.Value
If TextBox1.Value is integer then
numStocks and TextBox1.Value will be the same value.
If TextBox1.Value is not an integer then
numStocks will be an integer and TextBox1.Value will not.
Thus, they will not have the same value.
57. IS THE NUMBER AN INTEGER?
Let’s add this to
the code from
Exercise #4
This will be true
when TextBox1 is
not an integer
58. IS THE NUMBER AN INTEGER?
Test for correct
numerical values
59. IS THE NUMBER AN INTEGER?
If they enter a number between
0-100, assign numStocks the
value of the TextBox
If they do not enter a number
between 0-100, give an error
message and exit the Sub.
60. IS THE NUMBER AN INTEGER?
If a number between 0-100 is
entered, check if it is an integer
AFTER you assign numStocks the
value of TextBox1
64. IS IT A NUMBER?
Use the function called IsNumeric( )
IsNumeric (controlName.value)
The value of the function is either TRUE or FALSE
If TRUE, then the value of controlName is a number
If FALSE, then the value of controlName is not a number
If IsNumeric(controlName.value) Then
Code to run if controlName.Value is a number
Else
Code to run if controlName.Value is not a number
End if
65. EXAMPLE. IS IT A NUMBER?
Did they enter a number?
If not, give an error message and exit the sub
If IsNumeric(controlName.value) Then
Code to run if controlName.Value is a number
Else
MsgBox(“Your error message”)
Exit Sub
End if
67. IS WHAT THEY ENTERED CORRECT?
If the user enters a value you think may be wrong,
then ask them to confirm their response.
Confirm responses using a Yes/No Msgbox
MsgBox (“message”, vbYesNo)
The MsgBox returns a value: “vbYes” or “vbNo”
To save this value, assign it to a variable like this:
answer = MsgBox(“message”, vbYesNo)
68. EXAMPLE. YES/NO MSGBOX
The MsgBox returns a value
answer = MsgBox(“Are you sure you don’t want to make
an investment?”, vbYesNo)
If they click Yes, then
answer = vbYes
If they click No, then
answer = vbNo
69. EXAMPLE. YES/NO MSGBOX
If the user enters 0
(use a logic statement to figure this out)
then run this code.
70. EXERCISE #6
Use UserForm1 again. Complete this code which
checks for correct and incorrect textbox values:
1.
2.
3.
4.
Did they enter a number between 0-100?
If so, is it an integer?
Have they entered a non-numerical value?
Did they enter 0?
We’ve done 1-3 already. Now complete #4 using a
vbYesNo MsgBox.
Test your code to make sure it works in every
situation.
If your If-statements aren’t working, try stepping
through your code.
71. LEARNING OUTCOMES
You are ready to move on when…
LO29: You can read and understand the logic of a Select
Case statement. You can also construct a Select Case
statement.
LO30: You can read and understand the logic of an If-then
statement. You can also construct an If-then statement.
LO31: You can use logic statements to check for errors and
invalid entries.