< Day Day Up > |
It is not often that code will be completely free of error the very first time you write it, so it is inevitable that you will have to fix errors. Debugging is the term given to finding and fixing errors in code. There are two primary ways that code can fail. The first is that instructions might be written incorrectly. They might use the wrong syntax or have spelling errors. The second is that the instructions you have written are the wrong instructions. They might have perfect syntax, but the way they are ordered or structured may cause them not to do what you expect them to do. The VBE will try to help prevent the first type of error through built-in syntax checking. It assists in the second type by allowing you to set breakpoints and watches to closely monitor what the code is doing step-by-step. Syntax CheckingWhen you enter code and move the cursor off a line, the VBE automatically checks the syntax of the line. For example, if you omit Then from the end of an If statement, the VBE generates a compile error with the message Expected Then or GoTo . This feature of the VBE eliminates many errors that might otherwise be hard to find, before you even start testing. The VBE can detect a large number of syntax errors. Breakpoints, Watches, and the Immediate WindowWhen you have code with a large number of steps and you know only the initial state and the outcome, it is difficult to figure out where the root of your problem lies. The VBE provides the ability to view your code as it executes and to check the values of your variables . The main tools to do this are breakpoints, watches, and the Immediate window. The VBE lets you step line-by-line through code, but if you have a large amount of repetitive code, it could take a long time to get to the point in which you are interested. To facilitate this, you can set breakpoints in code. Breakpoints allow code to run freely up until the specific line in which the breakpoint is set. The code then stops, and you can evaluate the status of different variables or use the breakpoint to start stepping through the code line-by-line . To test code as part of debugging, follow these steps:
When you're testing code, you can also try typing ?Activecell.Task.Name . This can be very useful, but it only has a few properties. By using the Task property in this example, you can access all the task information for the task in the selected row. Other common and very useful properties of tasks that you need to work with are Work and Duration . In the Immediate window, go to a blank row and type ?T.Duration and then press Enter. The answer should be a rather large number that looks nothing like what you see in the Duration column of Project 2003. Project stores all Duration and Work values in minutes. To convert these to days, divide by 60 and then by the number of hours per day listed in the Calendar tab of the Options dialog box. The default number of hours per day is 8, so ?T.Duration/60/8 should display the correct number of days' duration. TIP ActiveProject.HoursPerDay should give you the number of hours per day to use in the formula instead of 8. Try typing ?T.Duration/60/ActiveProject.HoursPerDay in the Immediate window to confirm this. You have already learned that the variable T is Nothing until the For Each loop, when it points to each task in turn. You also need to be aware that the variable has no value at all in the Immediate window if the macro hasn't stopped at a breakpoint. If you type ?T.Name in the Immediate window when the code hasn't stopped at a breakpoint, an error is generated. T only points to a task after the For Each T In ActiveProject.Tasks statement has been executed. After End Sub is reached, T has no value, and any attempt to use it in the Immediate window fails. TIP Remember that if you want more information on a property or object, you can click it and press F1 to call up the help system. When the code has stopped at a breakpoint, notice that, in the gray border on the left of the VBE, the yellow line has a yellow arrow and the breakpoint has a red spot. Click the red spot to toggle the breakpoint off or on, or click anywhere on the line's code and press F9 again. You can also click and drag the yellow arrow to move the yellow line (changing which line will be executed next). Try moving it back to the For Each row. Press F8 again and again, and watch the code being stepped through. At any time, you can press F5 to have it run continuously to the next breakpoint or the end of the subroutine, whichever comes first. Enter a line like Set T=ActiveProject.Tasks(1) in the Immediate window and press Enter, and the line is executed. This is a good way to get a line of code working properly before you copy it back to the editing pane. Remember that when you click any instruction and press F9, the code can be made to stop. You then hover your mouse over any variable to get a ToolTip that displays its value, or you can use ? to print values in the Immediate window. You can also single-step through the code to see exactly what is happening, correcting the code as you go. You can also use Alt+Tab to swap between the VBE and Project (or any other program you are controlling from Project VBA) to look at what the code has done. There are two alternatives to using the Immediate window. The first is the Locals window, which you display by selecting Locals from the View menu. The Locals window contains the names , values, and properties of the objects that are being evaluated in code. Because it shows every possible piece of information about the objects, it can be a bit difficult to navigate through, but it is helpful if you aren't exactly sure of the specific property you are looking for. The second alternative is the Watch window, which you display by selecting Watch from the view menu. This window first appears with no watches set. To set a watch, highlight the item in the code that you want to watch, and then go to the Watch window, right-click, and select Add Watch. You can set a watch to display the value of an expression (just as you can get the value by hovering the mouse above the variable). You can also set a watch to break when the value of an expression you are watching turns either true or false. Figure 6 shows the code stopped where the expression "T is Nothing" has become true. Figure 6. The Watch window shows watches for an expression "T is Nothing" and for a variable.
If you know what you are looking for, using the Watch window is the simplest way to display the values you want. |
< Day Day Up > |