Excel 2019: How to Create Excel Macros to Automate Commands
Excel macros allow you to automate commands to simplify your tasks. Excel 2019 enables you to add an optional Developer tab to the Ribbon that contains its own Record Macro command button (among other command buttons that are very useful when doing more advanced work with macros). To add the Developer tab to the Excel 2019 Ribbon, follow these two steps:
- Click File → Options or press Alt+FT to open the Excel Options dialog box.
- Click Customize Ribbon, then select the Developer check box under Main Tabs in the Customize the Ribbon list box on the right side of the dialog box, and then click OK.
Even if you don’t add the Developer tab to the Ribbon, the Excel 2019 Status bar contains a Record Macro (to the immediate right of the Ready status indicator), and the View tab of the Ribbon contains a Macros command button with a drop-down menu containing a Record Macro option.
When you turn on the Excel macro recorder in the Record Macro dialog box — opened by clicking the Record Macro button on the Status bar (automatically added once you record your first macro), the Record Macro option on the Macros button’s drop-down menu (Alt+WMR), or even the Record Macro button on the Developer tab (Alt+LR) — the macro recorder records all your actions in the active worksheet or chart sheet when you make them.
The Excel macro recorder doesn’t record the keystrokes or mouse actions that you take to accomplish an action — only the VBA code required to perform the action itself. This means that mistakes that you make while taking an action that you rectify won’t be recorded as part of the macro; for example, if you make a typing error and then edit it while the macro recorder is on, only the corrected entry shows up in the macro without the original mistakes and steps taken to remedy them.
The Excel macros that you create with the macro recorder can be stored as part of the current workbook, in a new workbook, or in a special, globally available Personal Macro Workbook named PERSONAL.XLSB that’s stored in a folder called XLSTART on your hard drive. When you record an Excel macro as part of your Personal Macro Workbook, you can run that macro from any workbook that you have open. (This is because the PERSONAL.XLSB workbook is secretly opened whenever you launch Excel, and although it remains hidden, its macros are always available.) When you record macros as part of the current workbook or a new workbook, you can run those macros only when the workbook in which they were recorded is open in Excel.
When you create a macro with the macro recorder, you decide not only the workbook in which to store the macro but also what name and shortcut keystrokes to assign to the macro that you are creating. When assigning a name for your macro, use the same guidelines that you use when you assign a standard range name to a cell range in your worksheet. When assigning a shortcut keystroke to run the macro, you can assign
- The Ctrl key plus a letter from A to Z, as in Ctrl+Q
- Ctrl+Shift and a letter from A to Z, as in Ctrl+Shift+Q
You can’t, however, assign the Ctrl key plus a punctuation or number key (such as Ctrl+1 or Ctrl+/) to your macro.
To see how easy it is to create a macro with the macro recorder, follow these steps for creating a macro that enters the company name in 12-point, bold type and centers the company name across rows A through E with the Merge and Center feature:
- Open the Excel workbook that contains the worksheet data or chart you want your macro to work with. If you’re building a macro that adds new data to a worksheet (as in this example), open a worksheet with plenty of blank cells in which to add the data. If you’re building a macro that needs to be in a particular cell when its steps are played back, put the cell pointer in that cell.
- Click Record Macro button on the Status bar or Alt+WMR or Alt+LR if you have added the Developer tab to the Ribbon.
The Record Macro dialog box opens where you enter the macro name, define any keystroke shortcut, select the workbook in which to store the macro, and enter a description of the macro’s function.
- Replace the Macro1 temporary macro name by entering your name for the macro in the Macro Name text box.
Remember that when naming a macro, you must not use spaces in the macro name and it must begin with a letter and not some number or punctuation symbol. For this example macro, you replace Macro1 in the Macro Name text box with the name Company_Name.
Next, you can enter a letter between A and Z that acts like a shortcut key for running the macro when you press Ctrl followed by that letter key. Just remember that Excel has already assigned a number of Ctrl+letter keystroke shortcuts for doing common tasks, such as Ctrl+C for copying an item to the Clipboard and Ctrl+V for pasting an item from the Clipboard into the worksheet (see the Cheat Sheet for a complete list). If you assign the same keystrokes to the macro you’re building, your macro’s shortcut keys override and, therefore, disable Excel’s ready-made shortcut keystrokes.
- (Optional) Click the Shortcut key text box and then enter the letter of the alphabet that you want to assign to the macro.
For this example macro, press Shift+C to assign Ctrl+Shift+C as the shortcut keystroke (so as not to disable the ready-made Ctrl+C shortcut).
Next, you need to decide where to save the new macro that you’re building. Select Personal Macro Workbook on the Store Macro In drop-down list box to be able to run the macro anytime you like. Select This Workbook (the default) when you need to run the macro only when the current workbook is open. Select New Workbook if you want to open a new workbook in which to record and save the new macro.
- Click the Personal Macro Workbook, New Workbook, or This Workbook option on the Store Macro In drop-down list to indicate where to store the new macro.
For this example macro, select the Personal Macro Workbook so that you can use it to enter the company name in any Excel workbook that you create or edit.
Next, you should document the purpose and function of your macro in the Description list box. Although this step is purely optional, it is a good idea to get in the habit of recording this information every time you build a new macro so that you and your coworkers can always know what to expect from the macro when it’s run.
- (Optional) Click the Description list box and then insert a brief description of the macro’s purpose in front of the information indicating the date and who recorded the macro.
Now you’re ready to close the Record Macro dialog box and start recording your macro.
- Click OK to close the Record Macro dialog box.
The Record Macro dialog box closes, the square Stop Recording button appears on the Status bar, and the Record Macro option becomes Stop Recording on the Macros button’s drop-down menu and in the Code group on the Developer tab.
On the Macros button’s drop-down menu on the Ribbon’s View tab and Code group on the Developer tab, you find a Use Relative References option. You click this drop-down menu item or command button when you want the macro recorder to record the Excel macro relative to the position of the current cell. For this example macro, which enters the company name and formats it in the worksheet, you definitely need to click the Use Relative References button before you start recording commands. Otherwise, you can use the macro only to enter the company name starting in cell A1 of a worksheet.
- (Optional) Click the Use Relative References option on the Macros button’s drop-down menu on the View tab or click the Use Relative References button on the Developer tab if you want to be able to play back the macro anywhere in the Excel sheet.
- Select the cells, enter the data, and choose the Excel commands required to perform the tasks that you want recorded just as you normally would in creating or editing the current worksheet, using the keyboard, the mouse, or a combination of the two.
For the example macro, type the company name and click the Enter button on the Formula bar to complete the entry in the current cell. Next, click the Bold button and then click 12 on the Font Size drop-down list in the Font group on the Home tab. Finally, drag through cells A1:E1 to select this range and then click the Merge and Center command button, again on the Home tab.
After you finish taking all the actions in Excel that you want recorded, you’re ready to shut off the macro recorder.
- Click the Stop Recording button on the Status bar or select Stop Recording option on the View or Developer tab on the Ribbon.
The square Stop Recording button on the Status bar turns into a Record Macro button (with an icon showing a tiny worksheet with a circle in the left corner). This lets you know that the macro recorder is now turned off and no further actions will be recorded.
After you finish recording your first macro in Excel 2019, the Record Macro button continues to appear on the Status bar whenever you use the program. This means that you can click or tap this button to open the Record Macro dialog box whenever you need to create new macros rather than having to select the Record Macro option on the View or Developer tab of the Ribbon, as described in the previous steps.
Assigning Excel macros to the Ribbon and the Quick Access toolbar
If you prefer, instead of running a macro by selecting it in the Macro dialog box or by pressing shortcut keys you assign to it, you can assign it to a custom tab on the Ribbon or a custom button on the Quick Access toolbar and then run it by clicking that custom button.
To assign an Excel macro to a custom group on a custom Ribbon tab, you follow these steps:
- Click File → Options and then click the Customize Ribbon tab in the Excel Options dialog box (or press Alt+FTC).
Excel displays the Customize Ribbon pane in the Excel Options dialog box.
- Click Macros in the Choose Commands From drop-down list box on the left. Excel lists the names of all the macros created, both those in the current workbook and those that are saved in the PERSONAL.XLSB workbook, in the Choose Commands From list box.
- Click the name of the custom group on the custom tab to which you want to add the macro in the Main Tabs list box on the right.
If you haven’t already created a custom tab and group for the macro or need to create a new one, follow these steps:
- Click the New Tab button at the bottom of the Main Tabs list. Excel adds both a New Tab (Custom) and New Group (Custom) item to the Main Tabs list while at the same time selecting the New Group (Custom) item.
- Click the New Tab (Custom) item you just added to the Main Tabs.
- Click the Rename button at the bottom of the Main Tabs list box and then type a display name for the new custom tab before you click OK.
- Click the New Group (Custom) item right below the custom tab you just renamed.
- Click the Rename button and then type a display name for the new custom group before you click OK.
- In the Choose Commands From list box on the left, click the name of the macro you want to add to the custom group now selected in the Main Tabs list box on the right.
- Click the Add button to add the selected Excel macro to the selected custom group on your custom tab. If you want to rename the macro and/or assign it a new icon, click the Rename button and make these changes in the Rename dialog box before you click the OK button to close the Excel Options dialog box.
After you add a macro to the custom group of a custom tab, the name of the macro appears on a button on the custom tab of the Ribbon. Then, all you have to do to run the macro is click this command button.
To assign an Excel macro to a custom button on the Quick Access toolbar, follow these steps:
- Click the Customize Quick Access Toolbar button at the end of the Quick Access toolbar and then click More Commands on its drop-down menu. Excel opens the Excel Options dialog box with the Quick Access Toolbar tab selected.
- Click Macros in the Choose Commands From drop-down list box.
Excel lists the names of all the macros you created, both those in the current Excel workbook and those that are saved in the PERSONAL.XLSB workbook, in the Choose Commands From list box.
- Click the name of the macro to add to a custom button on the Quick Access toolbar in the Choose Commands From list box and then click the Add button.
- Click the Modify button to open the Modify Button dialog box if you want to change the display name and assign a different icon to the macro button.
- Click OK to close the Excel Options dialog box.
After you close the Excel Options dialog box, a custom button sporting its associated macro icon (the default with a standard command flowchart unless you changed it) appears on the Quick Access toolbar. To see the name of the Excel macro assigned to this custom macro button as a ScreenTip, position the mouse pointer over the button. To run the macro, click the button.