Google Sheets lets you automate repetitive tasks with macros, and you can then bind them to keyboard shortcuts to execute them quickly. They work using Google Apps Script to capture your actions for later use.
What are Macros?
A macro—or macroinstruction—is a specific sequence of actions that let you automate a series of steps to increase productivity. They work by recording your actions and saving them into a file that’s bound to the spreadsheet in which they were recorded.
When you record a macro in Google Sheets, it automatically creates an Apps Script with all the code to replicate your actions for you. This means you can create complex macros without knowing how to write code. The next time you run it, Sheets will do everything you did when you recorded the macro. Essentially, you’re teaching Google Sheets how to manipulate a document to your liking with a single command.
Macros are a powerful feature that can do basically anything Sheets is capable of doing. Here are just a few examples of its functionality:
- Apply formatting and styles.
- Create completely new spreadsheets.
- Use any Google Sheets function, toolbar, menu, or feature.
The sky is the limit.
How to Record a Macro in Google Sheets
Fire up a Google Sheet and click Tools > Macros > Record Macro.
This opens the recording menu at the bottom of the window, with two options for capturing your actions:
- Absolute References: The macro will do tasks only on the exact cells you record. If you italicize cell B1, the macro will only ever italicize B1 regardless of what cell you clicked on.
- Relative References: The macro will do tasks on the selected cells, regardless of where they are in the sheet. If you italicize B1 and C1, you can re-use the same macro to italicize cells D1 and E1 later.
Select whether you want an absolute or relative reference, then you can start to click, format, and teach Sheets what order you want these actions to replicate.
After you’ve captured all the actions for this macro, click “Save.”
Enter a name for your macro. Google also lets you create shortcuts for up to ten macros. If you want to bind a macro to a keyboard shortcut, enter a number from 0-9 in the space provided. When you finish, click “Save.”
If you need to tweak your macro’s name or shortcut, you can edit a macro by clicking Tools > Macros > Manage Macros.
From the window that opens, tweak as desired and then click “Update.”
The next time you press the shortcut associated with the macro, it will run without having to open the macro menu from the toolbar.
How to Run a Macro in Google Sheets
If your macro is an absolute reference, you can run the macro by pressing the keyboard shortcut or go to Tools > Macros > Your Macro and then click the appropriate option.
Otherwise, if your macro is a relative reference, highlight the cells in your spreadsheet on which you want the macro to run and then press the corresponding shortcut, or click on it from Tools > Macros > Your Macro.
How to Import Macros
As mentioned earlier, when you record a macro, it gets bound to the spreadsheet on which you recorded it. But what if you want to import a macro from another spreadsheet? While it’s not a straightforward and simple task, you can do it using this little workaround.
Because recorded macros are stored as functions in Google Apps Script, to import a macro, you need to copy the function and then paste it in the new sheet’s macro file.
Open the Google Sheet with the macro you want to copy and then click on Tools > Macros > Manage Macros.
Next, click the “More” icon next to the macro you’d like to copy and then click “Edit Script.”
All macros save to the same file, so if you have a couple of macros saved, you may have to sift through them. The function’s name is the same one you gave it when you created it.
Highlight the macro(s) you want to copy, then press Ctrl + C. Be sure to copy everything up to and including the closing semi-colon.
Now, open the other spreadsheet you’ll be importing the macro to and click Tools > Macros > Record Macro.
Immediately click “Save” without recording any actions to create a placeholder function in the sheet’s macro file for us. You’ll be deleting this a little later.
Click “Save” again.
Open Google Apps Script by clicking Tools > Script Editor, and then open the macros.gs file from the left pane. Delete the existing function and then press Ctrl + V to paste in the macro from your other Sheet.
Press Ctrl + S to save the script, close the tab, and return to your spreadsheet.
Your spreadsheet reads the macros.gs file and looks for changes made to it. If a new function is detected, you can use the Import feature to add a macro from another sheet.
Next, click Tools > Macros > Import.
Finally, click “Add Function” under the macro you want to add.
Unfortunately, you will have to bind the macro manually to a keyboard shortcut again. Just follow the instruction previously mentioned, and you’ll be all set to use this macro across multiple sheets.
That’s all there is to creating and using macros in Google Sheets. There’s no limit to the routines you can create for your spreadsheets to limit the amount of time you spend doing these repetitive tasks.