nested if statements in vba

13
# 1 Nested If Statements in VBA What is a compound condition that we evaluate? What is a Nested If statement? How do we use ElseIf? CS 105 Spring 2010

Upload: jamese

Post on 07-Jan-2016

35 views

Category:

Documents


0 download

DESCRIPTION

Nested If Statements in VBA. What is a compound condition that we evaluate? What is a Nested If statement? How do we use ElseIf ?. Compound Conditions -- Review. If intA = 26 intB = 34 intC = 16 Then intA < intB is TRUE intB < intC is FALSE - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Nested If Statements in VBA

# 1

Nested If Statements in VBA Nested If Statements in VBA

What is a compound condition that we evaluate?

What is a Nested If statement?

How do we use ElseIf?

CS 105 Spring 2010

Page 2: Nested If Statements in VBA

# 2CS 105 Spring 2010

Compound Conditions -- Review

If intA = 26 intB = 34 intC = 16 Then

intA < intB is TRUE

intB < intC is FALSE

(intA < intB) And (intB < intC) is FALSE

(intA < intB) Or (intB < intC) is TRUE

Not (intB < intC) is TRUE

Page 3: Nested If Statements in VBA

# 3CS 105 Spring 2010

Simple Use of NOT

If NOT (mintRow < 3) Then Exit Sub

End If

Page 4: Nested If Statements in VBA

# 4CS 105 Spring 2010

Example of NOT – Track Meet Scores Private Sub cmdNotExample_Click()

If Not (intUIUC < intMSU) And Not (intUIUC < intIU) Then Range(“C5").Value = "We won, We won!"End If

End Sub

Page 5: Nested If Statements in VBA

# 5CS 105 Spring 2010

Nested If Statements

• Use Nested If when you have multiple decisions, as in a decision tree.

• You use Case Statements when a variable has multiple values

(we will cover Case Statements next)

Page 6: Nested If Statements in VBA

# 6CS 105 Spring 2010

Putting it all together with Excel

We named this cell “Total”

Page 7: Nested If Statements in VBA

# 7CS 105 Spring 2010

What does this code do?

Private Sub cmdEvaluate_Click()If Range("Total").Value > 5000 Then

Range("Total").Interior.ColorIndex = 6 Else

If Range("Total").Value > 3000 ThenRange("Total").Interior.ColorIndex

= 8Else

Range("Total").Interior.ColorIndex = 4

End If End IfEnd Sub

Page 8: Nested If Statements in VBA

# 8CS 105 Spring 2010

If the first condition is True…

Private Sub cmdEvaluate_Click()If Range("Total").Value > 5000 Then

Range("Total").Interior.ColorIndex = 6 Else

If Range("Total").Value > 3000 ThenRange("Total").Interior.ColorIndex

= 8Else

Range("Total").Interior.ColorIndex = 4

End If End IfEnd Sub

Page 9: Nested If Statements in VBA

# 9CS 105 Spring 2010

What does this code do?

Private Sub cmdEvaluate_Click()If Range("Total").Value > 5000 Then

Range("Total").Interior.ColorIndex = 6ElseIf Range("Total").Value > 3000 Then

Range("Total").Interior.ColorIndex = 8Else

Range("Total").Interior.ColorIndex = 4End If

End Sub

Page 10: Nested If Statements in VBA

# 10CS 105 Spring 2010

Add the comments to an Else/If

Private Sub cmdEvaluate_Click()If Range("Total").Value > 5000 Then

'Yellow should show the profit! Range("Total").Interior.ColorIndex = 6

ElseIf Range("Total").Value > 3000 Then ' light blue gives us a warning Range("Total").Interior.ColorIndex = 8

Else 'We feel sick...green Range("Total").Interior.ColorIndex = 4

End If

End Sub

Page 11: Nested If Statements in VBA

# 11CS 105 Spring 2010

Nested If Flowchart

Nested If Flowchart

Execute nextstatement after End If in procedure

Test Condition

If

FalseTrue

Statements below the If, then go to below final End If

Test Condition

True

Statements below Else

Statements below ElseIf, then go to below final End If

ElseIf

False

Page 12: Nested If Statements in VBA

# 12

Testing our knowledge

If the condition is FALSE do we

• A. Skip over code and go to Else or ElseIf?

• B. Execute the next line of code?

CS 105 Spring 2010

Page 13: Nested If Statements in VBA

# 13CS 105 Spring 2010

To Summarize:

What is a compound condition that we evaluate?

What is a Nested If statement?

How do we use ElseIf?