Debugging in VBA (a HowTo)
Debugging in VBA (an HowTo)With the new compatibility of VBA with Autocad, some of you might want to explore the capacities of VBA. I think you should as the VBA language is strong, quick and in my opinion transparent. I’m a self thought VBA programmer, and especially in my first years I developed my capacities slow. I now believe I should have known more about debugging in VBA. I wrote this to help starters: Knowing how to debug one’s code is vital for sound programming. Frustration is probably unavoidable. Knowing how to debug will surely help avoid frustration too. As I failed to find a good tutorial on the web to point to, I decided to start this thread with my own experiences. I had to be short. Please feel free to make additions to it.----------------------------------------The purpose of debugging is to have detailed insight into what your code is doing with your variables and objects at any time during the execution of your code. ----------------------------------------1. Option ExplicitBefore you start I recommend you have the Option ‘Require Variable Declaration’ set in the Options dialog of the Visual Basic Editor (VBE). At the top of your module sheets the statement Option Explicit will now appear. This option will demand from you that you declare your variables before you use them. This will help you in preventing errors.This option is responsible for the error message “Variable not defined”. It points to variable names that are unknown. You might have forgotten to declare it. Other causes might range from a serious mistake to a simple typo.2. Compile your codeIn the Visual Basic Editor (VBE) you have the menu option Debug – Compile. I suggest you get used to compile your code before execution. It will find and highlight syntax mistakes in your code. Beware: The error messages are often difficult to interpret, especially for the beginner. If you do not understand the syntax mistake that VBE reports to you, try to run your code as it is not required to compile your code beforehand. Your code will run without being compiled and as a consequence stumble on the syntax mistakes your code contains while executing.3. Your code halts with an error messageWhen an error message pops up, you code is halted at the location the error is found. Select ‘Debug’ and use any of the techniques presented here to get to understand the mistake your code contains. 4. MsgBoxIf you have a variable that you suspect of causing mistakes, you’ll want to know what the value of the variable is during execution. You’ll include a ‘MsgBox variable’ statement in your code. Using the function MsgBox has the advantage that you can have the BricsCad window on top while you execute your code, and still be reported about the value of the variable. To prevent the execution of the rest of your code include an ‘Exit Sub’ statement right after it.There are more advanced techniques than MsgBox, all accessible in the Visual Basic Editor (VBE).5. Debug.PrintSimilar to the MsgBox is the statement ‘Debug.Print variable’. It writes the actual value of the variable into the Immediate Window that is part of the VBE. Ctrl+G will bring up that window. The execution of the code is not halted. The generated values can be evaluated after the execution of the macro.6. Setting BreakpointsAny line of code can be set as a breakpoint (except for comment lines). You click in the left margin of the code in the VBE to set or unset a breakpoint. A big round dot appears in the margin. ‘F9’ is a short-cut to toggle the breakpoint. Execution of your code will automatically halt at this place and you’ll be able to have a detailed look into the current state of your variables and objects and their values resp. properties. You’ll use for this the Locals Window and/or the Watch Window. You can continue with the execution by pressing F5. Pressing F8 can be used to go step by step through the code. You can set more than one breakpoint. Breakpoint can be very useful if set inside loops. 7. The Locals WindowThe Locals Window displays all your declared variables and objects in the current procedure, their values and their type. The Locals Windows is automatically updated. Declared variables might still be empty, containing “” or ‘Nothing’ etc. Objects might contain properties that are not yet initialised (
Comments
-
Yes, the VBA programming enviroment is great. I decided against LISP because of this. I also held off on a large programming project for over a year, waiting for the VBA-AutoCAD compatable version to be released.The reason for waiting is that I needed good documnentation and a tutorial. When the VBA became AutoCAD compatable, then I could use any VBA for AutoCAD book to help me along.Joe Dunfee
0