Editing Macros in the Visual Basic Editor
After you've created a macro, you don't necessarily have to rerecord it to change the way it behaves. In many cases, you may find it more efficient to change its behavior by simply editing its contents in Visual Basic Editor (also known as the VBA - as in Visual Basic for Applications - Editor for short).
Before you can use VBA Editor to edit a macro that you saved in your Personal Macro Workbook, you must first unhide this workbook by selecting PERSONAL.XLS in the Unhide dialog box (Window → Unhide).
To open a macro for editing in Visual Basic Editor, follow these general steps:
- Press Alt+F8 or choose Tools → Macros → Macro to open the Macro dialog box.
- Select the name of the macro that you want to edit in the Macro Name list box and then click the Edit button to open the module sheet containing
the macro in VBA Editor.
Excel opens Visual Basic Editor with the code for your macro displayed in the Code window - unless you select the name of a macro saved in the Personal Macro Workbook and this workbook is still hidden. In that case, Excel displays an alert dialog box telling you that you can't edit a hidden macro and informing you that you need to use the Window → Unhide command. You then need to click OK in the alert dialog box, press Esc to close the Macro dialog box, and then unhide the Personal Macro Workbook before you repeat these first two macro editing steps.
After you have the lines of code for the macro displayed in the Code window in Visual Basic Editor, you can edit any of its statements as needed. If you want to obtain a printout of the lines of code in your macro before you begin making changes, choose File → Print or press Ctrl+P. This action opens a Print dialog box with the Current Module option button selected in the Range section and the Code check box selected in the Print What section so that you just click OK to have Excel print all the statements in the macro.
- Edit the statements in the Code window of Visual Basic Editor as needed. When editing the macro's commands, remember
that you can use the Edit → Undo (Ctrl+Z) command to undo any deletion that you make by mistake. You can also find out what a particular
statement or property does in the macro by selecting it with the I-beam mouse pointer and pressing F1 or by clicking the Help button on the
After you finish editing the macro, you're ready to return to your spreadsheet where you can test out the modified macro and make sure that you haven't added some wacky, unwanted command to the macro or, even worse, crippled it so that it no longer runs at all.
- Click the View Microsoft Excel button at the beginning of the Standard toolbar or click the workbook's minimized button on the Windows
taskbar to return to the worksheet.
Select an appropriate or safe place in which to test your modified macro and then run it, either by pressing its shortcut keys or by pressing Alt+F8, clicking it in the Macro list box, and then clicking the Run button.
If something doesn't work as intended or if the macro doesn't work at all, return to the Visual Basic Editor and find and correct your error(s). Click the minimized Microsoft Visual Basic button on the Windows taskbar to return to the Visual Basic Editor and have a try at editing the code one more time.
If everything checks out and runs as planned, save your changes as outlined in Step 5.
- Press Ctrl+S or choose File → Save to save the changes to the modified macro if it's stored as part of the current workbook.
If you modified a macro saved in the Personal Macro Workbook, you have to exit Excel in order to save your changes to the macro. When you exit the program (Alt+F4 or File → Exit), Excel displays an alert dialog box asking if you want to save the changes you made to the personal.xls file. Be sure to click the Yes button to save the changes to your global macro before you close Excel.