Learn to use Macros to help you to automate work in Excel.
Excel's Automation tools, like macros, are covered extensively in our NYC Excel classes. For those outside New York, find and compare the best Excel classes near you or online Excel classes.
Macros
In this section, we'll discuss macros. Macros help you to automate the work you do in Excel so you don't have to manually repeat steps over and over again that are involved in creating your reports or creating your pivot tables or formatting inside of a spreadsheet.
So let's start with finding out where you can start to create a macro.
Getting Started
By default, if you head over to the View tab and go all the way over to the right, there is a Macros group. If you click on the dropdown, you will see commands for Viewing Existing Macros, Recording Macros and using something called Relative Reference. We'll take a look at all three options.
Developer Tab
Now, if you're working with macros on a regular basis, you'll probably want to add what's called the Developer tab. The Developer tab will give you more options when it comes to working with your macros.
So I'm going to right-click on the word View and choose Customize Ribbon. When I choose Customize Ribbon, I can see that there's a tab called Developer that's currently not checked. I want to add that to my ribbon. So I'm going to click the checkbox for Developer and then click OK.
Now I have a Developer ribbon tab, and if I click on it, I do not have to click the dropdown to access Macros, Recording Macros or Use Relative References. I can also access the Visual Basic editor and look at Macro Security.
Recording a Macro
So let's take a look at how you would record a macro.
First, you'll see that you can click the Record button on the Developer tab. You did have the Record button over on the dropdown. Here's another location where you can initiate the recording of a macro, regardless of what tab you're on. If you move down to the status bar, this icon here is an icon for initiating the recording of a macro.
Naming a Macro
Now I'll go to the Developer tab and click Record. Let's look at the Record Macro dialog box. We need to come up with a name for a macro, but we need to use a name that works. A name that will not work is a name that begins with a number.
But I'm actually going to break the rule here and attempt to do this just to show you what happens if you try to name a macro not using the proper naming conventions. When you click OK, Excel gives you a reminder of what all the best practices are for naming a macro.
So no harm, no foul. I'll go back and just give this macro an appropriate name. I'll call it "WordMacro."
Keyboard Shortcuts
I can apply a keyboard shortcut, but I wouldn't recommend you type the letter C here. If you press Control C, you will not copy anything. You will run this macro. Microsoft will override the default Control C command.
So you may be at a loss as to what keyboard shortcuts you can use because almost every letter is taken. Microsoft allows you to use the Shift key, and since this is a Word macro, I'll use Shift W. So the entire W's there, even though you might only see part of it, this is just a really small box.
You can store the macro in this workbook. You can store the macro in a brand new workbook, in which case the macro will be stored in another workbook and not this workbook. You can also store the macro in what's called a personal macro workbook, and this is an invisible workbook that will always be open in the background whenever you work in Excel.
We're going to stick with this workbook just to make things simple. You can write a description for your macro. This is useful if you're collaborating with someone or this is a project that you haven't worked with in a while and you want to remind yourself what this project was about.
Now, before I click OK, I want you to take a look at the Record Macro button up in the command bar. When I click OK, it now says Stop Recording. So my actions are now being recorded.
Just to let you know, when I click on Tabs, that is not part of the recording. So I don't have to worry about clicking on the wrong tab while I'm making a recording. I won't have to start over because this is not being recorded.
Now, our macro is simply going to type the word "word" in the cell. It's not revolutionary, but we just want to get a macro recorded that is very simple, and then we can test to see how it works.
So I'm going to type "word," and then I'm not going to press Enter. I'll press Control Enter. Control Enter, allows me to stay in the cell that I'm in. And that is our macro. I'll click Stop Recording.
Running a Macro
And now I want to test it. Now I use the keyboard shortcut Control Shift W. Let me test it to see if that works. It does. I'll go here, Control Shift W.
Now it may not be practical to share my macro with someone using that keyboard shortcut. If they add the Developer tab to their ribbon or they go to their View dialog box, they can click Macros, and there they can see the word Macro that I recorded, and they can click on it and choose Run.
Adding a Button
Another way that you can run a macro is you can add a button to your spreadsheet.
This is a good use of the Developer tab. If you go to the Controls group and click the dropdown underneath the Insert dialog box, the one that has the icon of a toolbox, you'll choose from Form Controls the very first button Form Control. Not ActiveX Controls, but Form Controls.
I'll click on the button, and I'll draw the shape of a button. The first thing you're going to be asked to do for this button is choose the macro you'd like to associate with that button. I'll click OK, and now I can go in and make an edit to the text, because Button 1 is a little too generic. I'm going to select the text and call this "Word Macro." I'll click outside of the button, and now it's active.
If I click anywhere in the spreadsheet, all I have to do to run the macro is click the Word Macro.
Quick Access Toolbar
One last way that you can run your macro, if you click the dropdown and go to More Commands, you'll see an option you probably never noticed before. When you click the dropdown for Popular Commands, you'll see Macros, and if you click that, you'll see any macros that you've created in this listing.
Now, I'm going to add it to my Quick Access Toolbar, and if I don't like this icon, I can choose Modify and choose from any of the icons that I see here. I'm going to go and pick the letter A since this is text and click OK. I'll click OK, and now if I want to run that macro, I can select anywhere in the sheet and simply click on the Quick Access Toolbar. Because of the way that I recorded the macro, I could select multiple cells and click the Quick Access Toolbar icon for that macro and apply that word across multiple cells.