Название | Excel Macros For Dummies |
---|---|
Автор произведения | Alexander Michael |
Жанр | Зарубежная образовательная литература |
Серия | |
Издательство | Зарубежная образовательная литература |
Год выпуска | 0 |
isbn | 9781119369271 |
To see this macro in action, delete the total row for both tables and do the following:
1. Select cell A1.
2. Select Macros from the Developer tab.
3. Find and select the AddTotalRelative macro.
4. Click the Run button.
5. Now select cell F1.
6. Select Macros from the Developer tab.
7. Find and select the AddTotalRelative macro.
8. Click the Run button.
Notice that this macro, unlike your previous macro, works on both sets of data. Because the macro applies the totals relative to the currently active cell, the totals are applied correctly.
For this macro to work, you simply need to ensure that
❯❯ You’ve selected the correct starting cell before running the macro.
❯❯ The block of data has the same number of rows and columns as the data on which you recorded the macro.
Hopefully, this simple example has given you a firm grasp of macro recording with both absolute and relative references.
Other Macro Recording Concepts
At this point, you should feel comfortable recording your own Excel macros. Here are some of other important concepts you’ll need to keep in mind when working with macros.
Beginning with Excel 2007, Excel workbooks were given the standard file extension .xlsx. Files with the .xlsx extension cannot contain macros. If your workbook contains macros and you then save that workbook as an .xlsx file, your macros are removed automatically. Excel warns you that macro content will be removed when saving a workbook with macros as an .xlsx file.
If you want to retain the macros, you must save your file as an Excel Macro-Enabled Workbook. This gives your file an .xlsm extension. The idea is that all workbooks with an .xlsx file extension are automatically known to be safe, whereas you can recognize .xlsm files as a potential threat.
With the release of Office 2010, Microsoft introduced significant changes to its Office security model. One of the most significant changes is the concept of trusted documents. Without getting into the technical minutia, a trusted document is essentially a workbook you have deemed safe by enabling macros.
If you open a workbook that contains macros in Excel 2010, you see a yellow bar message under the Ribbon stating that macros (active content) have, in effect, been disabled.
If you click Enable, it automatically becomes a trusted document. This means you no longer are prompted to enable the content as long as you open that file on your computer. The basic idea is that if you told Excel that you “trust” a particular workbook by enabling macros, it is highly likely that you will enable macros each time you open it. Thus, Excel remembers that you’ve enabled macros before and inhibits any further messages about macros for that workbook.
This is great news for you and your clients. After enabling your macros just one time, they won’t be annoyed at the constant messages about macros, and you won't have to worry that your macro-enabled dashboard will fall flat because macros have been disabled.
If the thought of any macro message coming up (even one time) unnerves you, you can set up a trusted location for your files. A trusted location is a directory that is deemed a safe zone where only trusted workbooks are placed. A trusted location allows you and your clients to run a macro-enabled workbook with no security restrictions as long as the workbook is in that location.
To set up a trusted location, follow these steps:
1. Select the Macro Security button on the Developer tab.
This activates the Trust Center dialog box.
2. Click the Trusted Locations button.
This opens the Trusted Locations menu (see Figure 1-6), which shows you all the directories that are considered trusted.
3. Click the Add New Location button.
4. Click Browse to find and specify the directory that will be considered a trusted location.
FIGURE 1-6: The Trusted Locations menu allows you to add directories that are considered trusted.
After you specify a trusted location, any Excel file opened from this location will have macros automatically enabled.
Most user-created macros are designed for use in a specific workbook, but you may want to use some macros in all your work. You can store these general-purpose macros in the Personal Macro Workbook so that they’re always available to you. The Personal Macro Workbook is loaded whenever you start Excel. This file, named personal.xlsb, doesn't exist until you record a macro using Personal Macro Workbook as the destination.
To record the macro in your Personal Macro Workbook, select the Personal Macro Workbook option in the Record Macro dialog box before you start recording. This option is in the Store Macro In drop-down list (refer to Figure 1-1).
If you store macros in the Personal Macro Workbook, you don’t have to remember to open the Personal Macro Workbook when you load a workbook that uses macros. When you want to exit, Excel asks whether you want to save changes to the Personal Macro Workbook.
The Personal Macro Workbook normally is in a hidden window to keep it out of the way.
When you create macros, you may want to have a clear and easy way to run each macro. A basic button can provide a simple but effective user interface.
As luck would have it, Excel offers a set of form controls designed specifically for creating user interfaces directly on spreadsheets. There are several different types of form controls, from buttons (the most commonly used control) to scrollbars.
The idea behind using a form control is simple: You place a form control on a spreadsheet and then assign a macro to it – that is, a macro you’ve already recorded. When a macro is assigned to the control, that macro is executed, or played, when the control is clicked.
Take a moment to create a button for the AddTotalRelative macro you created earlier. Here’s how:
1. Click the Insert button under the Developer tab. (See Figure 1-7.)
2. Select the Button Form Control from the drop-down list that appears.
3. Click the location where you want to place your button.
When you drop the button control onto your spreadsheet, the Assign Macro dialog box, shown in Figure 1-8, activates and asks you to assign a macro to this button.
4. Select the macro you want to assign to the button and then click OK.
FIGURE 1-7: You can find the form controls in the Developer tab.