Excel Macros For Dummies. Alexander Michael

Чтение книги онлайн.

Читать онлайн книгу Excel Macros For Dummies - Alexander Michael страница 3

Excel Macros For Dummies - Alexander Michael

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

redial those numbers with the touch of a button. Just as on a cell phone, you can record your actions in Excel while you perform them. While you record, Excel gets busy in the background, translating your keystrokes and mouse clicks to written code (also known as Visual Basic for Applications (VBA)). After a macro is recorded, you can play back those actions anytime you want.

      In this chapter, you’ll explore macros and learn how you can use macros to automate your recurring processes to simplify your life.

      Why Use a Macro?

      The first step in using macros is admitting you have a problem. Actually, you may have several problems:

      ❯❯ Problem 1 – Repetitive tasks: As each new month rolls around, you have to make the donuts (that is, crank out those reports). You have to import that data. You have to update those PivotTables. You have to delete those columns, and so on. Wouldn’t it be nice if you could fire up a macro and have those more redundant parts of your dashboard processes done automatically?

      ❯❯ Problem 2 – You’re making mistakes: When you go hand-to-hand combat with Excel, you’re bound to make mistakes. When you’re repeatedly applying formulas, sorting, and moving things around manually, there’s always that risk of catastrophe. Add to that the looming deadlines and constant change requests, and your error rate goes up. Why not calmly record a macro, ensure that everything is running correctly, and then forget it? The macro is sure to perform every action the same way every time you run it, reducing the chance of errors.

      ❯❯ Problem 3 – Awkward navigation: You 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

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