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

details.

       You cannot undo macro actions. When working in Excel, you can often undo the actions you've taken because Excel keeps a log (called the undo stack) recording your last 100 actions. However, running a macro automatically destroys the undo stack, so you can't undo the actions you take in a macro.

       You need to tweak the macros to fit your workbook. Many of the macros reference example sheet names and ranges that you may not have in your workbook. Be sure to replace references such as “Sheet 1” or Range (“A1”) with the sheet names and cell addresses you are working with in your own workbooks.

      Holy Macro Batman!

      Build a foundation for your macro skills with fundamental macro recording concepts.

      Get a solid understanding of the ground rules for using and distributing macros in Excel.

      Explore Excel’s coding environment with a deep-dive of the Visual Basic Editor.

      Explore how to leverage the Excel object model to start writing your own macros from scratch.

      Understand the roles played by variables, events, and error handling in macro development.

      Macro Fundamentals

      IN THIS CHAPTER

      

Choosing macros

      

Recording macros

      

Understanding macro security

      

Finding out where to store and how to run macros

      

Exploring macro examples

      A macro is essentially a set of instructions or code that you create to tell Excel to execute any number of actions. In Excel, macros can be written or recorded. The key word here is recorded.

      Recording a macro is like programming a phone number into your smartphone. You first manually dial and save a number. Then when you want, you can redial those numbers with the touch of a button. With macro recording, 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 code (also known as Visual Basic for Applications or VBA). After a macro is recorded, you can play back those actions anytime you want.

      In this chapter, you explore macros and find out how you can use macros to automate your recurring processes to simplify your life.

      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. With a macro you could have those more redundant parts of your monthly process 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. Or you could calmly record a macro, ensure that everything is running correctly, and then forget it. The macro performs 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.

      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 ⇒ Options.

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

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

      4 Click OK to return to Excel.

      FIGURE 1-1: The Record Macro dialog box.

       Macro Name: 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. You have to follow a few rules when naming a macro. The first character must be a letter. Generally, special characters other than underscore aren't allowed. And the total number of characters can't be more than 255, although hopefully you don't get close to that limit.

       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 if you use this field, 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). You can also choose New Workbook to tell Excel to create a new workbook to store the macro or Personal Macro Workbook, a special workbook used to store macros you want access to all the time. See “Storing and Running Macros” later in this chapter for more on the Personal Macro Workbook.

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

      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

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