Alexander Michael

Excel Macros For Dummies


Скачать книгу

often create reports for an audience that probably has a limited knowledge of Excel. It’s always helpful to make your reports more user-friendly. Macros can be used to dynamically format and print worksheets, navigate to specific sheets in your workbook, or even save the open document in a specified location. Your audience will appreciate these little touches that help make perusal of your workbooks a bit more pleasant.

      Macro Recording Basics

      To start recording your first macro, you need to first find the Macro Recorder, which is on the Developer tab. Unfortunately, Excel comes out of the box with the Developer tab hidden – you may not see it on your version of Excel at first. If you plan to work with VBA macros, you'll want to make sure that the Developer tab is visible. To display this tab

      1. Choose File ⇒ Excel Options.

      2. In the Excel Options dialog box, select Customize Ribbon.

      3. In the list box on the right, place a check mark next to Developer.

      4. Click OK to return to Excel.

Now that you have the Developer tab showing in the Excel Ribbon, you can start up the Macro Recorder by selecting Record Macro from the Developer tab. This activates the Record Macro dialog box, as shown in Figure 1-1.

       FIGURE 1-1: The Record Macro dialog box.

      Here are the four parts of the Record Macro dialog box:

      ❯❯ Macro Name: This should be self-explanatory. Excel gives a default name to your macro, such as Macro1, but you should give your macro a name more descriptive of what it actually does. For example, you might name a macro that formats a generic table as FormatTable.

      ❯❯ Shortcut Key: Every macro needs an event, or something to happen, for it to run. This event can be a button press, a workbook opening, or in this case, a keystroke combination. When you assign a shortcut key to your macro, entering that combination of keys triggers your macro to run. This is an optional field.

      ❯❯ Store Macro In: This Workbook is the default option. Storing your macro in This Workbook simply means that the macro is stored along with the active Excel file. The next time you open that particular workbook, the macro is available to run. Similarly, if you send the workbook to another user, that user can run the macro as well (provided the macro security is properly set by your user – more on that later in this chapter).

      ❯❯ Description: This is an optional field, but it can come in handy if you have numerous macros in a spreadsheet or if you need to give a user a more detailed description about what the macro does.

      With the Record Macro dialog box open, follow these steps to create a simple macro that enters your name into a worksheet cell:

      1. Enter a new single-word name for the macro to replace the default Macro1 name.

      A good name for this example is MyName.

      2. Assign this macro to the shortcut key Ctrl+Shift+N.

      You do this by entering uppercase N in the edit box labeled Shortcut Key.

      3. Click OK.

      This closes the Record Macro dialog box and begins recording your actions.

      4. Select any cell on your Excel spreadsheet, type your name into the selected cell, and then press Enter.

      5. Choose Developer ⇒ Code ⇒ Stop Recording (or click the Stop Recording button in the status bar).

Examining the macro

      The macro was recorded in a new module named Module1. To view the code in this module, you must activate the Visual Basic Editor. You can activate the VB Editor in either of two ways:

      ❯❯ Press Alt+F11.

      ❯❯ Choose Developer ⇒ Code ⇒ Visual Basic.

      In the VB Editor, the Project window displays a list of all open workbooks and add-ins. This list is displayed as a tree diagram, which you can expand or collapse. The code that you recorded previously is stored in Module1 in the current workbook. When you double-click Module1, the code in the module appears in the Code window.

      The macro should look something like this:

      Sub MyName()

      '

      ' MyName Macro

      '

      ' Keyboard Shortcut: Ctrl+Shift+N

      '

       ActiveCell.FormulaR1C1 = "Michael Alexander"

      End Sub

      The macro recorded is a Sub procedure named MyName. The statements tell Excel what to do when the macro is executed.

      Notice that Excel inserted some comments at the top of the procedure. These comments are some of the information that appeared in the Record Macro dialog box. These comment lines (which begin with an apostrophe) aren’t really necessary, and deleting them has no effect on how the macro runs. If you ignore the comments, you'll see that this procedure has only one VBA statement:

      ActiveCell.FormulaR1C1 = "Michael Alexander"

      This single statement causes the name you typed while recording to be inserted into the active cell.

Testing the macro

      Before you recorded this macro, you set an option that assigned the macro to the Ctrl+Shift+N shortcut key combination. To test the macro, return to Excel by using either of the following methods:

      ❯❯ Press Alt+F11.

      ❯❯ Click the View Microsoft Excel button on the VB Editor toolbar.

      When Excel is active, activate a worksheet. (It can be in the workbook that contains the VBA module or in any other workbook.) Select a cell and press Ctrl+Shift+N. The macro immediately enters your name into the cell.

      

In the preceding example, notice that you selected the cell to be altered before you started recording your macro. This step is important. If you select a cell while the macro recorder is turned on, the actual cell that you selected is recorded into the macro. In such a case, the macro would always format that particular cell, and it would not be a general-purpose macro.

Editing the macro

      After you record a macro, you can make changes to it (although you must know what you’re doing). For example, assume that you want your name to be bold. You could re-record the macro, but this modification is simple, so editing the code is more efficient. Press Alt+F11 to activate the VB Editor window. Then activate Module1 and insert the following statement before the End Sub statement:

      ActiveCell.Font.Bold = True

      The edited macro appears as follows:

      Sub MyName()

      '

      ' MyName Macro

      '

      ' Keyboard Shortcut: Ctrl+Shift+N

      '

       ActiveCell.Font.Bold = True

       ActiveCell.FormulaR1C1 = "Michael Alexander"

      End Sub

      Test this new macro, and you see that it performs as it should.

      Comparing Absolute and Relative Macro Recording

      Now that you’ve read about the basics of the Macro Recorder interface,