Spreadsheets are infinitely flexible—especially in Excel, one of the most powerful spreadsheet apps. Most people use only a small percentage of their seemingly countless possibilities, however. Yet it doesn't take years of training to take advantage of spreadsheets' power and the automation magic of Excel macros.
You likely already use functions like
=sum(A1:A5), the simple bits of text that add, average, and calculate your values. They're what make spreadsheets a powerful tool for crunching numbers and text. Macros are the next step: They're tools that automate simple tasks and help you get more done in less time. Here's how to unlock that new part of your Excel skill set by building your own macros in Excel.
This post was originally published in August 2016, but has been updated with additional tips
Macros are code that automate work in a program—they let you add your own tiny features and enhancements to help you accomplish exactly what you need to do, quickly with just a click of abutton. In a spreadsheet tool like Excel, macros can be especially powerful. Hidden behind the normal user interface, they are more powerful than standard functions you enter into a cell (e.g.
These macros make Excel work for you. They replace actions that you do manually—everything from formatting cells, copying values, and calculating totals. So with a few clicks you can quickly replace repetitive tasks.
To make these macros, you can simply record your actions in Excel to save them as repeatable steps or you can use Visual Basic for Applications (VBA), a simple programming language that's built into Microsoft Office. We'll show you how to use both below, as well as share examples of Excel macros to help you get started.
Learning how to automate Excel is one of the easiest ways to speed up your work–especially because Excel is used in so many work processes.
Say every week you export analytics data from your content management system (CMS) to create a report about your site. The only problem is, those data exports aren't always in an Excel-friendly format. They're messy and often include far more data than your report requires. This means you have to clean up empty rows, copy and paste data into the right place, and create your own charts to visualize data and make it print-friendly. All of these steps may take you hours to complete.
If there only was a way to press one button and let Excel do it for you in an instant… Well, can you guess what I’m about to say next?
All it requires is a little bit of time to set up a macro, and then that code can do the work for you automatically every time. It's not even as difficult as it sounds.
You already know your way around Excel, and are familiar with its grid of cells where you enter your text and functions. To build Excel macros, though, you'll need an extra tool that's built into Excel: the Visual Basic Editor.
Excel has a built-in tool for writing macros called the Visual Basic Editor—or VBA Editor for short. To open that, open a spreadsheet and use the shortcut
F11 (for Mac:
The new window that pops up is called the VBA Editor. It's where you'll edit and store all of your macros. Its layout may look a bit different from this screenshot, but you can move the windows around to the order you want. Just be sure to keep the Project Explorer pane open so you can easily edit your macros.
Your macros will be made up of “Modules," or files with your VBA code. You'll add a new module or open an existing one in the VBA Editor, then type in the code you want. To insert a module, click “Insert” and then click “Module”. You'll then see the blank space to write your code on the right.
There are two ways to make a macro: code it or record it. The main focus of this article is on the former, but recording a macro is so simple and handy, it's worth exploring too. Recording a macro is a good way of getting to know the basics of VBA. Later on, it serves as handy storage for code that you don’t need to memorize.
When you record a macro, you tell Excel to start the recording. Then you perform the tasks you want to be translated into VBA code. When you’re done, tell Excel to stop recording and you can use this new macro to repeat the actions you just performed again and again.
There are limitations to this, so you can't automate every task or become an expert in automation by only recording. You'll still need to type or edit code manually sometimes. But it's still a handy way to get started. Here's how:
1. Go to the “View” tab of the ribbon and click the tiny arrow below the “Macros” button.
2. Then click “Record Macro"
3. Type in the name of your macro and click “OK” to start the recording.
4. Perform the actions in your spreadsheet you want to be turned into a macro.
5. When you’re done, go to the “View” tab, click the tiny arrow below the “Record Macro” button again and select “Stop recording”.
Now, use the shortcut
Alt + F11 (for Mac:
Fn + Shift + F11) to open the VBA Editor, and double-click “Module 1” in the Project Explorer.
This is your first code! Amazing, right? You may not have written it yourself, but it’s still generated from your actions.
Yours probably look different than mine. Can you guess what my code does?
Sub Makebold is just the text
Sub followed by the name I entered when I started recording.
- The green line doesn’t actually do anything—it's a comment where you could add an explanation of what the macro does.
Selection.Font.Bold = True makes the values in the selected cells bold.
End sub simply tells Excel that the macro stops here.
Now, what will happen if I change the
True part of the third line to
False? The macro would then remove any bold formatting from the selection instead of making it bold.
That’s how you record a simple macro. But the real power of macros comes when you can write it yourself—so let's get started learning to write simple VBA code.
Macros are just bits of code in Excel that do your bidding. Once you write the code in the VBA Editor, you can run it and let the code work its magic on your spreadsheet. But what's even better is to build your macro into your spreadsheet, and the best tool for that is buttons.
So first, before we start coding, let's add a button to run our macro.
You can use various Excel objects as buttons for running macros, but I prefer to use a shape from the “Insert” tab. When you have inserted your shape, right click it and select “Assign Macro…” Then select the macro you want to run when the shape is clicked—perhaps the one you just made with a recording and save it by clicking “OK”.
Now, when you click the shape which we just turned into a button, Excel will run the macro without having to open the code each time.
There's one other thing to note before we get started: saving your spreadsheet with Macros. By default, Excel spreadsheet files with an
.xlsx extension cannot include macros. Instead, when you save your spreadsheet, select the "Excel Macro-Enabled Workbook (*.xlsm)" format, and add your file name as normal.
Go ahead and do that to save your spreadsheet before we start coding.
Now, let’s get started with actual coding!
Copying and pasting is the simplest way to move data around, but it's still tedious. What if your spreadsheet could do that for you? With a macro, it could. Let's see how to code a macro that will copy data and move it around in a spreadsheet.
Open the project file you downloaded earlier and make sure the “Copy, cut, and paste” sheet is selected. This is a sample employee database with the names, departments, and salaries of some employees.
Let’s try to copy all the data in columns A through C into D through F using VBA. First, let's look at the code we need:
Copying in VBA is quite easy. Just insert this code into the VBA Editor:
Range(“Insert range here”).Copy. Here's some examples:
Range(“A:C”).Copy ← copies column A through C
Range(“A1:C100”).Copy ← copies the range A1:C100
Remember when you recorded a macro before? The macro had
Sub Nameofmacro() and
End sub at the top and bottom line of the code. These lines must always be included. Excel makes that easy, too: When you type in "Sub" followed by the macro name in the beginning of the code, the
End sub is automatically inserted at the bottom line.
Pasting can be done in different ways depending on what you want to paste. 99% of the time, you’ll need one of these two lines of code:
Range(“The cell/area where you want to paste”).Pastespecial ← pastes as normal (formulas and formatting)
Range(“The cell/area where you want to paste”).Pastespecial xlPasteValues ← only pastes values
If you want to relocate your data instead of copying it, you need to cut it. Cutting is quite easy and follows the exact same logic as copying.
Here’s the code:
Range(“Insert range here”).Cut
When cutting, you can’t use the ‘PasteSpecial’ command. That means that you can’t paste values only, or formatting only. Therefore, you need these lines to paste your cells with VBA:
Range("Insert where you want to paste").Select
For example, here's the code you'd need to cut the range
A:C and paste it into
Copying, cutting, and pasting are simple actions that can be done manually without breaking a sweat. But when you copy and paste the same cells several times a day, a button that does it for you can save a bunch of time. Additionally, you can combine copying and pasting in VBA with some other cool code to do even more in your spreadsheet automatically.
I just showed you how to take a simple action (copying and pasting) and attach it to a button, so you can do it with a mouse click. That's just one automated action. When you have the code to repeat itself, though, it can do longer and more complex automation tasks in seconds.
Take a look at the “Loops” sheet in the project file. It’s the same data as in the previous sheet, but every third row of the data is now moved one column to the right. This type of faulty data structure is not unusual when exporting data from older programs.
This can take a lot of time to fix manually, especially if the spreadsheet includes thousands of rows instead of the small sample data in this project file.
Let’s make a loop that fixes it for you. Enter this code in a module, then look at the explanations below the picture:
For i = 1 To 500line means that the number of times the loop has run (represented by
i) is an increasing number that starts with 1 and ends with 500. This means that the loop will run 500 times. The number of times the loop should run depends on the actions you want it to do. Use your good sense here. 500 times is way too many for our sample dataset, but would fit perfectly if the database had 1500 rows of data.
When we run this macro, it will result in a neat dataset without any misplaced rows.
Logic is what brings a piece of code to life by making it more than just a machine that can do simple actions and repeat itself. Logic is what makes an Excel-sheet almost human—it lets it make intelligent decisions on its own. Let’s use that to automate things!
This section is about IF-statements which enables the “if-this-then-that” logic, just like the IF-function in Excel.
Let’s say the export from our website CMS was even more erroneous than expected. Every third row is still misplaced, but now, some of the misplaced rows are placed 2 columns to the right instead of 1 column to the right. Take a look at the sheet “IF-statement” in the project file to see what it looks like.
How do we take this into account in our macro? We add an IF-statement to the loop!
Let’s formulate what we want Excel to do:
We start in cell A1. Then we go three rows down (to cell A4, A7, A10, etc.) until there’s no more data. Every time we go three rows down we check this row to see if the data has been misplaced by 1 or 2 columns. Then move the data in the row either 1 or 2 columns to the left.
Now, let’s translate this into VBA code. We'll start with a simple loop, as before:
The only thing we need now is to write what should happen within the loop. This is the “go three rows down” part that we developed back in the section about loops. Now we’re adding an IF-statement that checks how much the data is misplaced and corrects it correspondingly.
This is the final code to copy into your module editor, with each step explained below:
Activecell.Offset(0,1)in VBA code) is blank (represented by
= "") then do something. This something is the exact same action as we did when we created the loop in the first place: deleting the active cell, and moving the active row one cell to the left (accomplished with the
Selection.Delete Shift:=xlToLeftcode). This time, we do it two times instead of one, because there are two blank cells in the left side of the row.
The IF-statement must always end with an
End If to tell Excel it's finished running. After the IF-statement, the loop can run again and again, repeating the IF-statement each time
Congratulations, you’ve just created a macro that can clean up messy data! See the animation below to see it in action (If you haven’t already tried it yourself).
Excel macros have only one problem: they're tied to your computer, and they can't run in the Excel Web App or on your mobile device. And they're best at working on data already in your spreadsheet, making it difficult to get new data from your other apps into your spreadsheet.
App integration tool Zapier can help. It connects the Office 365 for Business edition of Excel to hundreds of other apps—Stripe, Salesforce, Slack, and more—so you can log data to your spreadsheet automatically or start tasks in other apps right from Excel.
Here's how it works. Say you want to save your Typeform form entries to an Excel spreadsheet. Just create a Zapier account, and click the Make a Zap button in the top right corner. Then, select Typeform in the app picker, and set it to watch your form for new entries.
Test your Zap, then click Continue to add another step to your Zap. This time we'll select the Excel app, and choose to Add a Row to our spreadsheet. You could also update a row, or search your spreadsheet for a specific row if you wanted.
Now, choose your spreadsheet and worksheet, then click the + icon on the right of each spreadsheet row to select the correct form field to save to that spreadsheet row. Save and test your Zapier integration, then turn it on. Then every time your Typeform form gets filled out, Zapier will save that data to your Excel spreadsheet.
Here are some great ways to get started automating Excel with Zapier in a few clicks—or build your own Excel integrations to connect your spreadsheets to your favorite apps.
Now you’ve learned some of the most essential VBA tools to make a macro for cleaning up data and automating your work. Play around with the tricks and tools you've just learned, because they are the fundamentals for automation in VBA. Remember to use the macro recorder (and Google) when you feel you are in over your head.
To learn more, here are some extra resources to help you get the most out of Excel Macros:
- Learn more about how to repeat VBA code with loops
- Dig deeper into what you can do with
if statements and other logic jumps in Excel Macros
- Explore Microsoft's Excel VBA Reference to dig into everything you can do with VBA code and macros.
- Use Google Sheets instead of Excel? Check out our guide to Google Apps Script to learn how to automate Google Sheets
Try Zapier Today
“I was wasting hours each week doing data entry. Now Zapier handles it seamlessly.”
Build workflows with your apps.Try Zapier Free
Connect apps. Automate tasks. Get more done.Try Zapier Free
Zapier is the easiest way to automate powerful workflows with more than 750 apps.