Testing the Modified Macro

When you modify the recorded macro, it is quite possible that you may introduce errors. For example, you may delete an important line of code, or you may inadvertently remove or omit a necessary period. To make sure that your macro still works correctly after you're done editing, you must run it.

1. In the Visual Basic Editor Code window, place the cursor in any line of the WhatsInACell macro code, and choose Run | Run Sub/UserForm.

If you didn't introduce any problems during the modification of your macro, the macro will run smoothly and no errors will be reported. To see the result of your macro, you must switch to the Microsoft Excel window. To do this, click the button on the taskbar, or press Alt+F11.

If the Visual Basic Editor encounters an error during the execution of your macro, you will see a dialog box displaying the type of error found. Before you run macros, you must make sure that your macro can run in the worksheet that is currently selected. For example, if you try to run the WhatslnACell macro when a blank sheet is selected, you will get the "Run time error '1004'—No Cells were found" error message. Click the End button, and make sure that you select the correct worksheet before you try to run the macro again.

If the selected worksheet contains only cells with text and you try to run the WhatsInACell macro, Visual Basic will encounter a problem when attempting to select cells with numbers. The same 'No cells found" message will be displayed.

If you omit the period in With Selection.Font on running this line of code, Visual Basic will generate the "Run time error '424'—Object required" message. Click the Debug button in the message box, and you will be placed in the Code window. At this time, Visual Basic will enter into break mode and will use the yellow highlighter to indicate the line that it had trouble executing. As soon as you correct your error, Visual Basic may announce "This action will reset your project, proceed anyway?" Click OK in response to this message. Although you can edit code in break mode, some edits prevent continuing execution. After correcting the error, run the macro again, as there may be more errors to be fixed before the macro can run smoothly. You will find more information on how to handle VBA errors in Chapters 2 and 13.

Was this article helpful?

0 0

Post a comment