Читать книгу Excel VBA Programming For Dummies - Dick Kusleika - Страница 33

Running the Macro

Оглавление

Once you’ve finished writing your macro, it’s time to see if it actually works. The following are some of the ways to run a macro:

 Press the shortcut key. If you created a shortcut key sequence for your macro, you can run it by press those keys. For example, in the previous section you assigned Ctrl+Shift+N in the Macro Options dialog. If you hold down Ctrl and Shift, and press N, your macro will insert your name and the date and format them.

 Choose Developer ⇒ Code ⇒ Macros. This shows the Macro dialog where you can select your macro and click Run.

 Run it from the VBE. Open the VBE, place the cursor anywhere within your macro, and choose Run ⇒ Run Sub/UserForm from the menu (or press F5).

 Assign the macro to the Quick Access toolbar. Right-click on the Quick Access toolbar and choose Customize Quick Access Toolbar to show the Excel Options dialog. Choose Macros from the Choose commands from the drop-down dialog box and add your macro. Click OK to close the dialog. Now you can click the tool on the Quick Access toolbar to run the macro.

For your macro to run, the workbook that contains it must be open. If it’s closed, the macro won’t show up in the dialog box or in the VBE. The workbook doesn’t have to be active, however. You can activate a workbook and run code that’s contained in a different workbook. You can also store your macro in the Personal Macro Workbook (see Chapter 6) or in an add-in (see Chapter 21). Those are both types of files that are open in the background.

The NameAndTime macro you recorded in the previous section does not warn you if there is already data in the cells you are about to write data to. It’s not what you would call production-quality code.

Whatever way you choose to run your macro, run it and make sure it does what you expect it to. If it doesn’t, read on to see how to view and edit it.

Excel VBA Programming For Dummies

Подняться наверх