Macros don't always work perfectly . That is, you might make a mistake while recording the macro, or you might leave out a step. You don't need to worry about a macro that displays an error message because you can always fix those macro errors in Excel by editing, adding, and removing commands from the macro instructions. A macro might need additional commands or actions, or you might want to delete some command or action from the macro. What if you want to make changes to existing macro commands and actions or correct errors in a macro that doesn't run properly? No problem. You can make any of these changes to a macro by editing the macro. Looking at Macro CodeMacro instructions are written in Visual Basic, a fairly easy-to-use programming language. With the macro sheet in view onscreen, you can use Excel's editing commands to make changes to the Visual Basic instructions. You can remove macro commands, edit the specific contents of a cell in the macro worksheet, or even insert new commands into the middle of a macro. Of course, some changes require knowledge of Visual Basic. Specific commands that relate to actions that you want are described in the Microsoft Excel manual that comes with the software. You can view macro code in the macro sheet by switching to that sheet. To open the macro sheet, choose Tools, Macro, Macros. In the Macro dialog box, select the Font_change macro. Click the Edit button.
The Microsoft Visual Basic window appears, as shown in Figure 20.5. You should see the Visual Basic toolbar and three window panes:
Figure 20.5. The Microsoft Visual Basic window.
At the far right end of the Visual Basic toolbar, notice the line and column indicator: Ln X and Col X. These indicators tell you the line and column where the insertion point is located in the active pane. To activate a pane, simply click the pane. Editing the MacroYou can insert a command manually, remove a command, or edit a macro command on the macro sheet to make changes to the macro. You'll work with the Visual Basic Instructions pane on the right to make your changes. To get a better view of what you're doing in the Visual Basic Instructions pane, click the Maximize button in the upper-right corner of the pane. Excel enlarges the pane so that you see more macro instructions. The To Do exercise coming up helps you edit the macro. You change the macro's font size from 22 point to 28 point. To Do: Edit the Macro
Fixing a Macro with Step ModeWhen a macro doesn't work, the process of trying to find the problem and fixing it is called debugging . When you use Step Mode to debug a macro, Visual Basic displays a yellow arrow in the left border of the Visual Basic Instruction pane and highlights in yellow the macro instruction on the line it's pointing to. Read the instruction carefully to see whether it contains any errors, including typos. To use Step Mode to debug a macro, in the Macros dialog box, select the macro you want to debug. Click Step Into. If you're already in the Visual Basic Editor, choose Debug in the Visual Basic menu bar. Then you have three Step Mode choices:
Use these Step Mode commands to step through the macro instructions and pinpoint the location of any errors. |