L06 VBA intro3 best practice.pptx - Excel Modeling VBA Intro Best Practices Dr Ronald K CHUNG FINA0404/3351 – Spreadsheet Modelling in Finance

L06 VBA intro3 best practice.pptx - Excel Modeling VBA...

This preview shows page 1 - 11 out of 40 pages.

Excel Modeling: VBA Intro: Best Practices Dr. Ronald K. CHUNG FINA0404/3351 – Spreadsheet Modelling in Finance
Image of page 1

Subscribe to view the full document.

Building Good VBA Models (Ch 18, S) Error Free Realistic Flexible Easy to Provide Inputs Data Validation Good Output Production Judicious Formatting Statements are Easy to Read and Understand Well Organized Robust Good Documentation, including Comments in the Code
Image of page 2
4 Basic Types of Errors Syntax Errors After finish writing/editing a statement and press return or move the cursor away, VBA immediately checks the statement for syntax errors and automatically fixes the errors if possible. Compiler Errors Before it can run a code, VBA has to compile it, i.e., translating the code into a language that the computer understands Many of these errors may also originate in typing mistakes. E.g., use “Option Explicit” but forget to declare a variable in a Dim statement, or a variable you misspelled that VBA cannot recognize. Logical Errors errors in the programmer’s reasoning produce wrong results
Image of page 3

Subscribe to view the full document.

4 Basic Types of Errors Runtime Errors: occur during the execution of a program VBA cannot evaluate/execute a statement E.g., the denominator happens to be zero VBA displays a dialog box with an error code (number) and a brief description of the error, but often these descriptions are not very helpful. The dialog box generally has 3 active buttons. Debug button: VBA will highlight the code line where execution stopped and go into break mode, which is helpful for debugging End button: will end execution Help button: will display the online help topic for the particular runtime error.
Image of page 4
Errors Highlighted yellow: VBA trying to run this, BUT Syntex errors… “easy” (if you know the syntex, AND, the procedure will not work) E.g., Range( cell (3,1),cells(6,1) v [ cell S (3,1)] Compilation error… Add… Option Explicit , before Sub Run time error… (logic… wrong output) 5
Image of page 5

Subscribe to view the full document.

Exercise: In addition to the name of the analyst, we want to add a label “Analyst Name” in Cell B2, and Bold the analyst name, yellow background with blue font. The following was an analyst’s first shot at it… 6
Image of page 6
Ready, Camera, …to start Sub AnalystName 1() Range("A1").ColumnWidth = 2 Range("B1") = "Analyst name" Cell(1,2).ColumnWidth = 1 Range("C2") = "Ronald K. CHUNG" Selection.Font.bold = True Selection.ColumnWidth = 20 Selection.Font.Color = -65536 Selection.Interior.Color = 6750207 End Sub it did not quite work, what happened? How can we fix it? 7
Image of page 7

Subscribe to view the full document.

Simple Debugging F8 line-by-line debug (step-into) For intermediate actions: 1. “Comment out” 2. Insert one or a few MsgBox to display the values of a few key variables (Ch 38, B) Cannot really do F8 line-by-line Breakpoint
Image of page 8
Debugging with large programs Click on the vertical bar on the line you want the pgm to run to (or F9)… and stop F8 to go to the next line
Image of page 9

Subscribe to view the full document.

Debugging How fast you can debug will mostly depend on experience and understanding of the finance and mathematics of the problem.
Image of page 10
Image of page 11
  • Spring '17
  • Dialog box, Software bug

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

Ask Expert Tutors You can ask 0 bonus questions You can ask 0 questions (0 expire soon) You can ask 0 questions (will expire )
Answers in as fast as 15 minutes