Logic &
Error checking LECTURE 7
Logic statements
Select Case
If – statements
Error checking
Yes/No Message Box
SUMMARY
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
USING LOGIC
Using logic in your code
SELECT CASE
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.
LECTURE 7 STUDENT EXAMLE.XLSM
EXAMPLE 1. SELECT CASE
Testing the value of ComboBox1
ComboBox1
EXAMPLE 1. SELECT CASE
ComboBox1 Case Is = “ItemONE”
Is ComboBox1.Value = “ItemONE”
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
EXAMPLE 1. SELECT CASE
ComboBox1 Case Is = “ItemONE” Case Is = “ItemTWO”
Does ComboBox1.Value = “ItemTWO”
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
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
EXAMPLE 1. SELECT CASE
ComboBox1 Case Is = “ItemONE” Case Is = “ItemTWO”
Give two examples of when Case Else
would be true
EXAMPLE 1. SELECT CASE
ComboBox1
Give two examples of when Case Else
would be true
Case Else Case Else
EXAMPLE 2. SELECT CASE
When will ‘Case Else’ run?
EXAMPLE 2. SELECT CASE
How can we prevent errors from non-numeric
entries?
EXAMPLE 2. SELECT CASE
We test for correct entries rather than
incorrect entries
EXAMPLE 2. SELECT CASE
Just ‘Case’ not ‘Case Is’ for a range like this.
EXAMPLE 2. SELECT CASE
Case Else takes care of every value not between 0-100.
Select Case Name what is being tested
Case Is = Condition 1 … Case Is = Condition 2 … Case Else … End Select
The words in BOLD are VBA key words, so don’t change them.
SELECT CASE. GENERAL FORM.
For example: • Name of control • Name of variable • Name of range
Select Case Name what is being tested
Case Is = Condition 1 … Case Is = Condition 2 … Case Else … End Select
The words in BOLD are VBA key words, so don’t change them.
SELECT CASE. GENERAL FORM.
You can have as many conditions as you need
Select Case Name what is being tested
Case Is = Condition 1 … Case Is = Condition 2 … Case Else … End Select
The words in BOLD are VBA key words, so don’t change them.
SELECT CASE
An operator: =, >, <, <>
Select Case Name what is being tested
Case Is = Condition 1 … Case Is = Condition 2 … Case Else … End Select
The words in BOLD are VBA key words, so don’t change them.
SELECT CASE
Omit if Condition is a range (like example 2).
IF-THEN STATEMENTS
Logic test – Tests IF something is True.
If B is only one line of code, then you can write:
IF – THEN STATEMENTS
If Then
End If
A
B Can be multiple
lines of code
If Then A B
IF A is True, THEN do B
If x < 25 Then y = x + 17
End If
EXAMPLE. IF – THEN.
IF A is True, THEN do B
If x < 25 Then y = x + 17
OR
What happens when 𝑥 ≥ 25?
If x < 25 Then y = x + 17 MsgBox (“y = ” & y)
End If
EXAMPLE. IF – THEN.
IF A is True, THEN do B
Multiple lines of code so we MUST use this syntax
Cannot use one-line syntax
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
End If
A
B
EXERCISE #1. LOGIC.
Logic tests – Tests IF something is True.
IF – THEN – ELSE STATEMENTS
If Then
End If
A
B
IF A is True, THEN do B
If Then
Else End If
A
B
C
IF A is True, THEN do B, ELSE do C
If B and C are only one line of code, you can write:
IF – THEN-ELSE STATEMENTS
Can be multiple lines of code
If Then
Else End If
A
B
C
IF A is True, THEN do B, ELSE do C
If Then Else A B C
If x < 25 Then y = x + 17
Else y = x - 4
End If
EXAMPLE. IF – THEN – ELSE.
If x < 25 Then y = x + 17 Else y = x - 4
OR
IF A is True, THEN do B, ELSE do C
When will y = x – 4?
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.
EXERCISE #2. IF – THEN – ELSE.
If Then
Else End If
A
B
C
We are assuming that the user will only enter a
number and not any other non-numeric value
causing an error.
If x < 25 Then y = x + 17
Else If x > 100 Then y = x - 4
End If End If
EXAMPLE. IF – THEN – ELSE.
When will y = x – 4?
If x < 25 Then y = x + 17
Else If x > 100 Then y = x - 4
End If End If
EXAMPLE. IF – THEN – ELSE.
If x < 25 Then y = x + 17
ElseIf x > 100 Then y = x - 4
End If
IF – THEN-ELSEIF STATEMENTS
IF A is True, THEN do B IF C is True, THEN do D
IF A and C are False, THEN do E
If Then
Elseif Then End If
A
B
D
C
If Then
Elseif Then Else End If
A
B
D
E
C
Optional
Open Module1 and Exercise3()
Re-write the two If-Then statements so that you have one If-Then-Elseif statement
EXERCISE #3. IF – THEN – ELSE.
If x < 75 Then y = 5*x
End If
If x > 200 Then y = 2*x
End If
If x < 75 Then y = 5*x
Elseif x > 200 Then y = 2*x
End If
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-THEN AND SELECT CASE
Select Case x Case Is < 75 y = 5*x
Case Is > 200 y = 2*x
End Select
If x < 75 Then y = 5*x
Elseif x > 200 Then y = 2*x
Else y = x End If
IF-THEN AND SELECT CASE
Select Case x Case Is < 75 y = 5*x
Case Is > 200 y = 2*x
Case Else y = x
End Select
OR
AND LOGICAL OPERATORS
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
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?
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?
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?
USING LOGICAL OPERATORS – AND
If x < 25 Then
If x > 0 Then y = x + 5
End If
End If
If x > 0 AND x < 25 Then y = x + 5
End If
Both must be true
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.
EXERCISE #4. IF – THEN – ELSE.
We are assuming that the user will only enter a
number and not any other non-numeric value
causing an error.
Open Module1 and Exercise5()
Rewrite the following.
Decide if you should use AND or OR
EXERCISE #5. USING LOGICAL OPERATORS
ERROR CHECKING
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
WHAT ‘ERRORS’ SHOULD I CHECK FOR?
We did this already in Exercise #4
Did they enter an integer number?
If not, give an error message and exit the sub
If yes, then continue
WHAT ‘ERRORS’ SHOULD I CHECK FOR?
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.
CHECKING FOR ERRORS USING LOGIC
Testing if a number is an integer or not.
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
IS THE NUMBER AN INTEGER?
Dim numStocks as Integer numStocks = TextBox1.Value No matter what value is assigned to numStocks,
it will be rounded to an integer value
What is the value of numStocks?
IS THE NUMBER AN INTEGER?
Dim numStocks as Integer numStocks = TextBox1.Value No matter what value is assigned to numStocks,
it will be rounded to an integer value
What is the value of numStocks? numStocks = 8
IS THE NUMBER AN INTEGER?
Dim numStocks as Integer numStocks = TextBox1.Value No matter what value is assigned to numStocks,
it will be rounded to an integer value
What is the value of numStocks?
IS THE NUMBER AN INTEGER?
Dim numStocks as Integer numStocks = TextBox1.Value No matter what value is assigned to numStocks,
it will be rounded to an integer value
What is the value of numStocks? numStocks = 10
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.
IS THE NUMBER AN INTEGER?
This will be true when TextBox1 is
not an integer
Let’s add this to the code from
Exercise #4
IS THE NUMBER AN INTEGER?
Test for correct numerical values
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.
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
IS THE NUMBER AN INTEGER?
IS THE NUMBER AN INTEGER?
Same code using Select Case for the outer logic
statement
Testing if a value is a number.
Use the function called IsNumeric( )
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
IS IT A NUMBER?
If IsNumeric(controlName.value) Then
Else
End if
Code to run if controlName.Value is a number
Code to run if controlName.Value is not a number
IsNumeric (controlName.value)
Did they enter a number?
If not, give an error message and exit the sub
EXAMPLE. IS IT A NUMBER?
If IsNumeric(controlName.value) Then
Else MsgBox(“Your error message”) Exit Sub End if
Code to run if controlName.Value is a number
Use this to confirm user entries or clicks
MSGBOX VBYESNO
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
The MsgBox returns a value: “vbYes” or “vbNo”
To save this value, assign it to a variable like this:
answer = MsgBox(“message”, vbYesNo)
IS WHAT THEY ENTERED CORRECT?
MsgBox (“message”, vbYesNo)
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
EXAMPLE. YES/NO MSGBOX
EXAMPLE. YES/NO MSGBOX
If the user enters 0 (use a logic statement to figure this out)
then run this code.
Use UserForm1 again. Complete this code which checks for correct and incorrect textbox values:
1. Did they enter a number between 0-100?
2. If so, is it an integer?
3. Have they entered a non-numerical value?
4. 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.
EXERCISE #6
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.
LEARNING OUTCOMES
THE END