MgtOp-556-Topic-1-Set-3 - MgtOp 556Advanced Business...

Info icon This preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
MgtOp 556—Advanced Business Modeling Professor Munson Topic 1 VBA Programming in Excel for Decision Support Systems (Set 3—Control Logic and Loops) “The programming tools discussed in [chapter 7] are arguably the most important tools in VBA or any other programming language.” Albright, S. Christian, VBA for Modelers, 5 th Ed., Cengage Learning, 2016, p. 143
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Control Logic and Loops Control logic instructs the program to perform a task or tasks based on one or more conditions 1. If Construction Redirects the program when a condition holds 2. Case Construction Redirects the program based on multiple mutually exclusive conditions (cases) “Looping” instructs the program to perform a repetitive task potentially many times. In VBA, we use FOR and DO . 1. For Loop Performs a specified number of iterations using a counting variable to keep track 2. For Each Loop Performs an iteration for every object in a collection (e.g., for each cell in a range) 3. Do Loop Performs an iteration either while some condition holds or until some condition occurs
Image of page 2
If Construct Provides instruction based on whether or not a condition holds. A condition is any expression that is either true or false . Four Versions 1. Single-line Ex: If Range(“D4”).Value < 4 Then Range(“D4”).Font.Color = vbRed These can include an optional Else condition: If Range(“F9”).Value < 5 Then Range(“A9”).Font.Strikethrough _ = True Else Range(“A9”).Interior.Color = vbYellow 2. If-Then-Else-End Ex: If Range(“F4”).Value > Range(“F5”).Value Then Range(“A5:F5”).Font.Strikethrough = True Else Range(“A4:F4”).Font.Strikethrough = True End If
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
3 . If-Then-ElseIf-Else-End Ex: Dim max As Double max = WorksheetFunction.MAX(Range(“F4:F10”)) If Range(“F4”).Value = max Then MsgBox “I’m going to Arizona State!” ElseIf Range(“F5”).Value = max Then MsgBox “I’m going to CWU!” ElseIf Range(“F6”).Value = max Then MsgBox “I’m going to Duke!” ElseIf Range(“F7”).Value = max Then MsgBox “I’m going to Gonzaga!”
Image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern