1 / 13

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 ?. Compound Conditions -- Review. If intA = 26 intB = 34 intC = 16 Then intA < intB is TRUE intB < intC is FALSE

jamese
Download Presentation

Nested If Statements in VBA

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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

  2. 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 CS 105 Spring 2010

  3. Simple Use of NOT If NOT (mintRow < 3) Then Exit Sub End If CS 105 Spring 2010

  4. 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 CS 105 Spring 2010

  5. Nested If Statements • UseNested Ifwhen you have multiple decisions, as in a decision tree. • You useCase Statements when a variable has multiple values (we will cover Case Statements next) CS 105 Spring 2010

  6. Putting it all together with Excel We named this cell “Total” CS 105 Spring 2010

  7. 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 Then Range("Total").Interior.ColorIndex = 8 Else Range("Total").Interior.ColorIndex = 4 End If End If End Sub CS 105 Spring 2010

  8. 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 Then Range("Total").Interior.ColorIndex = 8 Else Range("Total").Interior.ColorIndex = 4 End If End If End Sub CS 105 Spring 2010

  9. What does this code do? Private Sub cmdEvaluate_Click() If Range("Total").Value > 5000 Then Range("Total").Interior.ColorIndex = 6 ElseIf Range("Total").Value > 3000 Then Range("Total").Interior.ColorIndex = 8 Else Range("Total").Interior.ColorIndex = 4 End If End Sub CS 105 Spring 2010

  10. 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 CS 105 Spring 2010

  11. If Test Condition False True ElseIf Statements below the If, then go to below final End If Test Condition True False Statements below ElseIf, then go to below final End If Statements below Else Execute next statement after End If in procedure Nested If Flowchart CS 105 Spring 2010

  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

  13. To Summarize: What is a compound condition that we evaluate? What is a Nested If statement? How do we use ElseIf? CS 105 Spring 2010

More Related