- What Are Excel Macros?
- Why Macros?
- How to Build Your First Excel Macros
- How to Code Your Own Excel Macros
- Connect Excel to Other Apps with Integrations
What Are Excel Macros?
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.
Why Use Excel Macros?
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.
How to Build Your First Excel Macro
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.
Meet The VBA 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.
How to Record an Excel Macro
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.
How to Code Your Own Excel Macros
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.
Add a Button to Run Your 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 Cells with VBA
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 Cells with VBA
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
Cutting Cells with VBA
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.
Adding Loops to VBA
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:
- This line makes sure the loop starts at the top-left cell in the sheet and not accidentally messes the data up by starting somewhere else.
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.
- This line recognizes the active cell and tells Excel to move 3 rows down and select that cell, which then becomes the new active cell. If it was every fourth row that was misplaced in our data, instead of every third, we could just replace the 3 with a 4 in this line.
- This line tells Excel what to do with this newly selected cell. In this case, we want to delete the cell in such manner that the cells to the right of the cell are moved left. That is achieved with this line. If we wanted to do something else with the misplaced rows, this is the place to do it. If we wanted to delete every third row entirely, then the line should’ve been:
- This line tells Excel that there are no more actions within the loop. In this case, 2 and 5 are the frame of the loop and 3 and 4 is the actions within the loop.
When we run this macro, it will result in a neat dataset without any misplaced rows.
Adding Logic to VBA
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:
- This is the first part of the IF-statement. It says that if the cell right of the active cell (or
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.
- If the above is not true, and the cell right of the active cell is not blank, then the active cell is blank. Therefore, we only need to delete the active cell and move the active row one cell to the left one time.
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).
Automate Excel Without Macros
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.
Manage Your Spreadsheet Data
Save Form Entries to an Excel Spreadsheet
Log Data to an Excel Spreadsheet
Do Work From Your Spreadsheet
Go Build Your Own Macros!
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