SlideShare a Scribd company logo
1 of 72
Download to read offline
LECTURE 7

Logic &
Error checking
SUMMARY
Logic statements
 Select Case

 If – statements

Error checking

Yes/No Message Box
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
SELECT CASE

Using logic
in your
code
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.
EXAMPLE 1. SELECT CASE

ComboBox1

Testing the value of
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

Case Else

Case Else

Give two examples
of when Case Else
would be true
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. 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.
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.
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.
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.
IF-THEN STATEMENTS
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
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?
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
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
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
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
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
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.
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?
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
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
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
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
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
LOGICAL OPERATORS

OR
AND
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

Both must be true

If x > 0 AND x < 25 Then
y= x+5
End If
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.
EXERCISE #5. USING LOGICAL OPERATORS
Open Module1 and Exercise5()
Rewrite the following.
Decide if you should use AND or OR
ERROR CHECKING
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
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
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.
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

What is the value of numStocks?

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

What is the value of numStocks?
numStocks = 8

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

What is the value of numStocks?

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

What is the value of numStocks?
numStocks = 10

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

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?

Let’s add this to
the code from
Exercise #4

This will be true
when TextBox1 is
not an integer
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.
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
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
MSGBOX VBYESNO

Use this to
confirm
user
entries or
clicks
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)
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
EXAMPLE. YES/NO MSGBOX

If the user enters 0
(use a logic statement to figure this out)
then run this code.
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.
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.
THE END

More Related Content

What's hot

Improper integrals IN MATH
Improper integrals IN MATHImproper integrals IN MATH
Improper integrals IN MATHRONAK SUTARIYA
 
Control Statements in Java
Control Statements in JavaControl Statements in Java
Control Statements in JavaNiloy Saha
 
Pairwise testing technique-Made easy
Pairwise testing technique-Made easyPairwise testing technique-Made easy
Pairwise testing technique-Made easySamee Ahmed Indikar
 
Project in TLE
Project in TLEProject in TLE
Project in TLEPGT_13
 
D-Pubb-TestingExperience_Issue_28_2014-12_Berta
D-Pubb-TestingExperience_Issue_28_2014-12_BertaD-Pubb-TestingExperience_Issue_28_2014-12_Berta
D-Pubb-TestingExperience_Issue_28_2014-12_BertaBerta Danilo
 
AVB201.2 Microsoft Access VBA Module 2
AVB201.2 Microsoft Access VBA Module 2AVB201.2 Microsoft Access VBA Module 2
AVB201.2 Microsoft Access VBA Module 2Dan D'Urso
 
Report Group 4 Constants and Variables
Report Group 4 Constants and VariablesReport Group 4 Constants and Variables
Report Group 4 Constants and VariablesGenard Briane Ancero
 
Ch05 Visual Aids
Ch05 Visual AidsCh05 Visual Aids
Ch05 Visual Aidskhaifuture
 
A Brief Introduction to Type Constraints
A Brief Introduction to Type ConstraintsA Brief Introduction to Type Constraints
A Brief Introduction to Type ConstraintsRaffi Khatchadourian
 
Indetermidiate forms
Indetermidiate formsIndetermidiate forms
Indetermidiate formsPreetshah1212
 
Object oriented programming16 boolean expressions and selection statements
Object oriented programming16 boolean expressions and selection statementsObject oriented programming16 boolean expressions and selection statements
Object oriented programming16 boolean expressions and selection statementsVaibhav Khanna
 
Operators , Functions and Options in VB.NET
Operators , Functions and Options in VB.NETOperators , Functions and Options in VB.NET
Operators , Functions and Options in VB.NETShyam Sir
 

What's hot (19)

Savitch ch 03
Savitch ch 03Savitch ch 03
Savitch ch 03
 
Improper integrals IN MATH
Improper integrals IN MATHImproper integrals IN MATH
Improper integrals IN MATH
 
Control Statements in Java
Control Statements in JavaControl Statements in Java
Control Statements in Java
 
Pairwise testing technique-Made easy
Pairwise testing technique-Made easyPairwise testing technique-Made easy
Pairwise testing technique-Made easy
 
Cis160 Final Review
Cis160 Final ReviewCis160 Final Review
Cis160 Final Review
 
Project in TLE
Project in TLEProject in TLE
Project in TLE
 
Savitch Ch 03
Savitch Ch 03Savitch Ch 03
Savitch Ch 03
 
D-Pubb-TestingExperience_Issue_28_2014-12_Berta
D-Pubb-TestingExperience_Issue_28_2014-12_BertaD-Pubb-TestingExperience_Issue_28_2014-12_Berta
D-Pubb-TestingExperience_Issue_28_2014-12_Berta
 
AVB201.2 Microsoft Access VBA Module 2
AVB201.2 Microsoft Access VBA Module 2AVB201.2 Microsoft Access VBA Module 2
AVB201.2 Microsoft Access VBA Module 2
 
Report Group 4 Constants and Variables
Report Group 4 Constants and VariablesReport Group 4 Constants and Variables
Report Group 4 Constants and Variables
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
Ch05 Visual Aids
Ch05 Visual AidsCh05 Visual Aids
Ch05 Visual Aids
 
Operators
OperatorsOperators
Operators
 
expressions
expressionsexpressions
expressions
 
A Brief Introduction to Type Constraints
A Brief Introduction to Type ConstraintsA Brief Introduction to Type Constraints
A Brief Introduction to Type Constraints
 
Indetermidiate forms
Indetermidiate formsIndetermidiate forms
Indetermidiate forms
 
Object oriented programming16 boolean expressions and selection statements
Object oriented programming16 boolean expressions and selection statementsObject oriented programming16 boolean expressions and selection statements
Object oriented programming16 boolean expressions and selection statements
 
Operators , Functions and Options in VB.NET
Operators , Functions and Options in VB.NETOperators , Functions and Options in VB.NET
Operators , Functions and Options in VB.NET
 
management
managementmanagement
management
 

Viewers also liked

Oxidative breakage of cellular dna by plant polyphenols
Oxidative breakage of cellular dna by plant polyphenolsOxidative breakage of cellular dna by plant polyphenols
Oxidative breakage of cellular dna by plant polyphenolsTaqprimer institute
 
Media task two 2
Media  task two 2Media  task two 2
Media task two 2teonamaud
 
Unit 8 assignment 2b
Unit 8 assignment 2bUnit 8 assignment 2b
Unit 8 assignment 2bteonamaud
 

Viewers also liked (6)

1c- Unit 8
1c- Unit 81c- Unit 8
1c- Unit 8
 
Oxidative breakage of cellular dna by plant polyphenols
Oxidative breakage of cellular dna by plant polyphenolsOxidative breakage of cellular dna by plant polyphenols
Oxidative breakage of cellular dna by plant polyphenols
 
Media task two 2
Media  task two 2Media  task two 2
Media task two 2
 
Unit 8 assignment 2b
Unit 8 assignment 2bUnit 8 assignment 2b
Unit 8 assignment 2b
 
MA3696 Lecture 8
MA3696 Lecture 8MA3696 Lecture 8
MA3696 Lecture 8
 
MA3696 Lecture 9
MA3696 Lecture 9MA3696 Lecture 9
MA3696 Lecture 9
 

Similar to MA3696 Lecture 7

Ch5 Selection Statements
Ch5 Selection StatementsCh5 Selection Statements
Ch5 Selection StatementsSzeChingChen
 
Variable, constant, operators and control statement
Variable, constant, operators and control statementVariable, constant, operators and control statement
Variable, constant, operators and control statementEyelean xilef
 
Advanced VB: Review of the basics
Advanced VB: Review of the basicsAdvanced VB: Review of the basics
Advanced VB: Review of the basicsrobertbenard
 
Advanced VB: Review of the basics
Advanced VB: Review of the basicsAdvanced VB: Review of the basics
Advanced VB: Review of the basicsrobertbenard
 
If and nested i fs
If and nested i fsIf and nested i fs
If and nested i fsPaulCPLD
 
CONTROL STRUCTURE IN VB
CONTROL STRUCTURE IN VBCONTROL STRUCTURE IN VB
CONTROL STRUCTURE IN VBclassall
 
ICDL Advanced Excel 2010 - Tutorial
ICDL Advanced Excel 2010 - TutorialICDL Advanced Excel 2010 - Tutorial
ICDL Advanced Excel 2010 - TutorialMichael Lew
 
03 Logical functions.pdf
03 Logical functions.pdf03 Logical functions.pdf
03 Logical functions.pdfRizwanAli988729
 
Reportgroup4 111016004939-phpapp01
Reportgroup4 111016004939-phpapp01Reportgroup4 111016004939-phpapp01
Reportgroup4 111016004939-phpapp01Nurhidayah Mahmud
 
Decision structures chpt_5
Decision structures chpt_5Decision structures chpt_5
Decision structures chpt_5cmontanez
 
Working with comparison operators
Working with comparison operatorsWorking with comparison operators
Working with comparison operatorsSara Corpuz
 
Vb.Net 01 To 03 Summary Upload
Vb.Net 01 To 03 Summary UploadVb.Net 01 To 03 Summary Upload
Vb.Net 01 To 03 Summary UploadHock Leng PUAH
 

Similar to MA3696 Lecture 7 (20)

Ch5 Selection Statements
Ch5 Selection StatementsCh5 Selection Statements
Ch5 Selection Statements
 
Using decision statements
Using decision statementsUsing decision statements
Using decision statements
 
Excel IF, IFs.ppt
Excel IF, IFs.pptExcel IF, IFs.ppt
Excel IF, IFs.ppt
 
Variable, constant, operators and control statement
Variable, constant, operators and control statementVariable, constant, operators and control statement
Variable, constant, operators and control statement
 
Introduction to Java
Introduction to JavaIntroduction to Java
Introduction to Java
 
Advanced VB: Review of the basics
Advanced VB: Review of the basicsAdvanced VB: Review of the basics
Advanced VB: Review of the basics
 
Advanced VB: Review of the basics
Advanced VB: Review of the basicsAdvanced VB: Review of the basics
Advanced VB: Review of the basics
 
How to Program
How to ProgramHow to Program
How to Program
 
Programming note C#
Programming note C#Programming note C#
Programming note C#
 
If and nested i fs
If and nested i fsIf and nested i fs
If and nested i fs
 
CONTROL STRUCTURE IN VB
CONTROL STRUCTURE IN VBCONTROL STRUCTURE IN VB
CONTROL STRUCTURE IN VB
 
ICDL Advanced Excel 2010 - Tutorial
ICDL Advanced Excel 2010 - TutorialICDL Advanced Excel 2010 - Tutorial
ICDL Advanced Excel 2010 - Tutorial
 
03 Logical functions.pdf
03 Logical functions.pdf03 Logical functions.pdf
03 Logical functions.pdf
 
Reportgroup4 111016004939-phpapp01
Reportgroup4 111016004939-phpapp01Reportgroup4 111016004939-phpapp01
Reportgroup4 111016004939-phpapp01
 
Decision structures chpt_5
Decision structures chpt_5Decision structures chpt_5
Decision structures chpt_5
 
Pseudocode By ZAK
Pseudocode By ZAKPseudocode By ZAK
Pseudocode By ZAK
 
Working with comparison operators
Working with comparison operatorsWorking with comparison operators
Working with comparison operators
 
Vb.Net 01 To 03 Summary Upload
Vb.Net 01 To 03 Summary UploadVb.Net 01 To 03 Summary Upload
Vb.Net 01 To 03 Summary Upload
 
Vba Class Level 1
Vba Class Level 1Vba Class Level 1
Vba Class Level 1
 
MS Excel 2010 tutorial 5
MS Excel 2010 tutorial 5MS Excel 2010 tutorial 5
MS Excel 2010 tutorial 5
 

More from Brunel University

More from Brunel University (6)

MA3696 Lecture 6
MA3696 Lecture 6MA3696 Lecture 6
MA3696 Lecture 6
 
MA3696 Lecture 5
MA3696 Lecture 5MA3696 Lecture 5
MA3696 Lecture 5
 
Ma3696 Lecture 3
Ma3696 Lecture 3Ma3696 Lecture 3
Ma3696 Lecture 3
 
Ma3696 Lecture 2
Ma3696 Lecture 2Ma3696 Lecture 2
Ma3696 Lecture 2
 
Ma3696 Lecture 0
Ma3696 Lecture 0Ma3696 Lecture 0
Ma3696 Lecture 0
 
Ma3696 Lecture 1
Ma3696 Lecture 1Ma3696 Lecture 1
Ma3696 Lecture 1
 

Recently uploaded

EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonJericReyAuditor
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 

Recently uploaded (20)

EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lesson
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 

MA3696 Lecture 7

  • 2. SUMMARY Logic statements  Select Case  If – statements Error checking Yes/No Message Box
  • 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.
  • 6. EXAMPLE 1. SELECT CASE ComboBox1 Testing the value of ComboBox1
  • 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
  • 14. EXAMPLE 2. SELECT CASE When will ‘Case Else’ run?
  • 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.
  • 50. Testing if a number is an integer or not.
  • 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
  • 61. IS THE NUMBER AN INTEGER?
  • 62. IS THE NUMBER AN INTEGER? Same code using Select Case for the outer logic statement
  • 63. Testing if a value is a number.
  • 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
  • 66. MSGBOX VBYESNO Use this to confirm user entries or clicks
  • 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.