Wednesday, September 26, 2012

Excel VBA - Debug Macros


You may have heard of the technique called debugging before. With this technique you can find errors in yourExcel VBA code before you execute the code. To show you how to debug a macro, execute the following steps.

1. Launch the Visual Basic Editor.
2. Create the following incorrect macro:

Create a Macro Error

2. Place your cursor before Private.
3. There are two ways to debug a macro. Either press F8 or choose Debug from the Menu and then Step Into. Debug your macro. The first line will turn yellow.

Debug the Code

4. Press F8 again. The second code line will turn yellow. As long as the debugger thinks everything is okay, nothing will happen.
5. Press F8 twice. The following error will show up.

A Run-time error

6. It says that the object doesn't support the property or method. The Range object has a property called Value. Value isn't spelled correctly here, so the debugger doesn't recognize the property. More about objects, properties and methods in the Object, Properties and Methods chapter. You may find debugging a lot of work at this stage, but it will definitely pay off once your programs become more complicated.

No comments:

Post a Comment