Top Banner
Logic & Error checking LECTURE 7
72
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: MA3696 Lecture 7

Logic &

Error checking LECTURE 7

Page 2: MA3696 Lecture 7

Logic statements

Select Case

If – statements

Error checking

Yes/No Message Box

SUMMARY

Page 3: MA3696 Lecture 7

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

Page 4: MA3696 Lecture 7

Using logic in your code

SELECT CASE

Page 5: MA3696 Lecture 7

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

Page 6: MA3696 Lecture 7

EXAMPLE 1. SELECT CASE

Testing the value of ComboBox1

ComboBox1

Page 7: MA3696 Lecture 7

EXAMPLE 1. SELECT CASE

ComboBox1 Case Is = “ItemONE”

Is ComboBox1.Value = “ItemONE”

Page 8: MA3696 Lecture 7

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

Page 9: MA3696 Lecture 7

EXAMPLE 1. SELECT CASE

ComboBox1 Case Is = “ItemONE” Case Is = “ItemTWO”

Does ComboBox1.Value = “ItemTWO”

Page 10: MA3696 Lecture 7

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

Page 11: MA3696 Lecture 7

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

Page 12: MA3696 Lecture 7

EXAMPLE 1. SELECT CASE

ComboBox1 Case Is = “ItemONE” Case Is = “ItemTWO”

Give two examples of when Case Else

would be true

Page 13: MA3696 Lecture 7

EXAMPLE 1. SELECT CASE

ComboBox1

Give two examples of when Case Else

would be true

Case Else Case Else

Page 14: MA3696 Lecture 7

EXAMPLE 2. SELECT CASE

When will ‘Case Else’ run?

Page 15: MA3696 Lecture 7

EXAMPLE 2. SELECT CASE

How can we prevent errors from non-numeric

entries?

Page 16: MA3696 Lecture 7

EXAMPLE 2. SELECT CASE

We test for correct entries rather than

incorrect entries

Page 17: MA3696 Lecture 7

EXAMPLE 2. SELECT CASE

Just ‘Case’ not ‘Case Is’ for a range like this.

Page 18: MA3696 Lecture 7

EXAMPLE 2. SELECT CASE

Case Else takes care of every value not between 0-100.

Page 19: MA3696 Lecture 7

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

Page 20: MA3696 Lecture 7

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

Page 21: MA3696 Lecture 7

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: =, >, <, <>

Page 22: MA3696 Lecture 7

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).

Page 23: MA3696 Lecture 7

IF-THEN STATEMENTS

Page 24: MA3696 Lecture 7

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

Page 25: MA3696 Lecture 7

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?

Page 26: MA3696 Lecture 7

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

Page 27: MA3696 Lecture 7

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.

Page 28: MA3696 Lecture 7

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

Page 29: MA3696 Lecture 7

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

Page 30: MA3696 Lecture 7

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?

Page 31: MA3696 Lecture 7

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.

Page 32: MA3696 Lecture 7

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?

Page 33: MA3696 Lecture 7

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

Page 34: MA3696 Lecture 7

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

Page 35: MA3696 Lecture 7

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

Page 36: MA3696 Lecture 7

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

Page 37: MA3696 Lecture 7

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

Page 38: MA3696 Lecture 7

OR

AND LOGICAL OPERATORS

Page 39: MA3696 Lecture 7

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

Page 40: MA3696 Lecture 7

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?

Page 41: MA3696 Lecture 7

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?

Page 42: MA3696 Lecture 7

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?

Page 43: MA3696 Lecture 7

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

Page 44: MA3696 Lecture 7

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.

Page 45: MA3696 Lecture 7

Open Module1 and Exercise5()

Rewrite the following.

Decide if you should use AND or OR

EXERCISE #5. USING LOGICAL OPERATORS

Page 46: MA3696 Lecture 7

ERROR CHECKING

Page 47: MA3696 Lecture 7

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

Page 48: MA3696 Lecture 7

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?

Page 49: MA3696 Lecture 7

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

Page 50: MA3696 Lecture 7

Testing if a number is an integer or not.

Page 51: MA3696 Lecture 7

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

Page 52: MA3696 Lecture 7

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?

Page 53: MA3696 Lecture 7

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

Page 54: MA3696 Lecture 7

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?

Page 55: MA3696 Lecture 7

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

Page 56: MA3696 Lecture 7

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.

Page 57: MA3696 Lecture 7

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

Page 58: MA3696 Lecture 7

IS THE NUMBER AN INTEGER?

Test for correct numerical values

Page 59: MA3696 Lecture 7

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.

Page 60: MA3696 Lecture 7

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

Page 61: MA3696 Lecture 7

IS THE NUMBER AN INTEGER?

Page 62: MA3696 Lecture 7

IS THE NUMBER AN INTEGER?

Same code using Select Case for the outer logic

statement

Page 63: MA3696 Lecture 7

Testing if a value is a number.

Page 64: MA3696 Lecture 7

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)

Page 65: MA3696 Lecture 7

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

Page 66: MA3696 Lecture 7

Use this to confirm user entries or clicks

MSGBOX VBYESNO

Page 67: MA3696 Lecture 7

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)

Page 68: MA3696 Lecture 7

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

Page 69: MA3696 Lecture 7

EXAMPLE. YES/NO MSGBOX

If the user enters 0 (use a logic statement to figure this out)

then run this code.

Page 70: MA3696 Lecture 7

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

Page 71: MA3696 Lecture 7

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

Page 72: MA3696 Lecture 7

THE END