Chapter 10
In This Chapter
Introducing macros
Recording macros
Setting up trusted locations for your macros
Adding macros to your dashboards and reports
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 cellphone. You first manually dial and save a number. Then when you want, you can redial those numbers with the touch of a button. Just as on a cellphone, 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 you record a macro, you can play back those actions anytime you want.
In this chapter, you explore macros and see how you can use them to automate recurring processes to simplify your life.
The first step in using macros is admitting you have a problem. Actually, you may have several problems:
Here are some ideas for macros that make things easier for everyone:
Obviously, you can perform each of these examples in Excel without the aid of a macro. However, your audience will appreciate these little touches that help make perusal of your dashboard a bit more pleasant.
If you’re a beginner to dashboard automation, you’re unlikely to be able to write the VBA code by hand. Without full knowledge of Excel’s object model and syntax, writing the needed code would be impossible for most beginning users. This is where recording a macro comes in handy. The idea is that you record an action and then run the macro every time you want that action performed.
To begin, you first need to unhide the Developer tab. You can find the full macro toolset in Excel on the Developer tab, which is initially hidden. You have to explicitly tell Excel to make it visible. To enable the Developer tab, follow these steps:
Click the Customize Ribbon button.
In the list box on the right, you see all available tabs.
Now that you have the Developer tab visible on the Ribbon, select it and click the Record Macro command. This opens the Record Macro dialog box, as shown in Figure 10-2.
Here are the four fields in the Record Macro dialog box:
In this first example, enter AddDataBars into the Macro Name field and select This Workbook from the Store Macro In drop-down menu; see Figure 10-3. Click OK.
Excel is now recording your actions. While Excel is recording, you can perform any actions you want. In this scenario, you record a macro to add Data Bars to a column of numbers.
Follow along using these steps:
Go to the Developer tab and click the Stop Recording command.
At this point, Excel stops recording. You now have a macro that replaces the data in C1:C21 with Data Bars. Now you record a new macro to remove the Data Bars.
Go to the Developer tab and click the Stop Recording command.
Again, Excel stops recording. You now have a new macro that removes conditional formatting rules from cells C1:C21.
To see your macros in action, select the Macros command from the Developer tab. The dialog box in Figure 10-5 appears, allowing you to select the macro you want to run. Select the AddDataBars macro and click the Run button.
If all goes well, the macro plays back your actions to a T and applies the Data Bars as designed; see Figure 10-6.
You can now call up the Macro dialog box again and test the RemoveDataBars macro shown in Figure 10-7.
When you create macros, you want to give your audience a clear and easy way to run each macro. A button, used directly on the dashboard or report, can provide a simple but effective user interface.
Excel Form controls enable you to create user interfaces directly on your worksheets, simplifying work for your users. Form controls range from buttons (the most commonly used control) to scroll bars and check boxes.
For a macro, you can place a Form control in a worksheet and then assign that 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, every time the control is clicked.
Take a moment to create buttons for the two macros (AddDataBars and RemoveDataBars) that you create earlier in this chapter. Here’s how:
Click the location where you want to place your button.
When you drop the Button control into the worksheet, the Assign Macro dialog box, as shown in Figure 10-9, opens and asks you to assign a macro to this button.
Select the macro that you want to assign.
In this case, select the AddDataBars macro and click OK.
Keep in mind that all controls on the Form Controls menu work in the same way as the command button, in that you assign a macro to run when the control is selected.
With the release of Office 2007, 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.
It’s important to note that Microsoft has created a separate file extension for workbooks that contain macros.
Workbooks created in Excel 2010 and later versions have the default 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. Of course, Excel warns you that macro content will be disabled 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. All workbooks with an .
xlsx file extension are automatically known to be safe, whereas you can recognize .xlsm files as a potential threat.
When you open a workbook that contains macros in Excel, you get a message in the form of a yellow bar under the Ribbon stating that macros (active content) have in effect been disabled.
If you click Enable Content, it automatically becomes a trusted document. You are then no longer prompted to enable the content as long as you open that file on your computer. If you told Excel that you trust a particular workbook by enabling macros, it’s highly likely that you will enable macros every 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 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:
Click the Trusted Locations button.
This step opens the Trusted Locations menu shown in Figure 10-10. There, you see all the directories that Excel considers trusted.
Click Browse to find and specify the directory that will be considered a trusted location.
After you specify a trusted location, any Excel file that’s opened from this location will have macros automatically enabled. Have your clients specify a trusted location and use your Excel files from there.
Covering the fundamentals of building and using macros is one thing. Coming up with good ways to incorporate them into your reporting processes is another. Take a moment to review a few examples of how you can implement macros in your dashboards and reports.
The most common use of macros is navigation. Workbooks that have many worksheets or tabs can be frustrating to navigate. To help your audience, you can create some sort of a switchboard, like the one shown in Figure 10-11. When users click the Example 1 button, they’re taken to the Example 1 sheet.
Creating a macro to navigate to a sheet is quite simple:
Assign the macro to a button.
If you need help assigning a macro to a button, check out the “Running Your Macros” section, earlier in this chapter.
In the example illustrated in Figure 10-12, macros allow a user to change the perspective of the chart simply by selecting any one of the buttons shown.
Figure 10-13 reveals that the chart is actually a pivot chart tied to a pivot table. The recorded macros assigned to each button are doing nothing more than rearranging the pivot table to slice the data using various pivot fields.
Here are the high-level steps needed to create this type of setup:
You can fire your new macros in turn to see the pivot field dynamically move back and forth.
The two earlier macro examples demonstrate that you can record any action that you find of value. That is, if you think users would appreciate a certain feature being automated for them, why not record a macro to do so?
In Figure 10-14, notice that you can filter the pivot table for the top or bottom 20 customers. Because the steps to filter a pivot table for the top and bottom 20 have been recorded, anyone can get the benefit of this functionality without knowing how to do it themselves. Also, recording specific actions allows you to manage risk a bit. That is to say, you’ll know that your users will interact with your reports in a method that has been developed and tested by you.
This not only saves them time and effort, but also allows users who don’t know how to take these actions to benefit from them.
Figure 10-15 demonstrates how you can give your audience a quick and easy way to see the same data on different charts. Don’t laugh too quickly at the uselessness of this example. It’s not uncommon to be asked to see the same data different ways. Rather than take up real estate, just record a macro that changes the chart type. Your clients can switch views to their hearts’ content.
18.117.107.40