Creating Macros in Google Sheets: A Practical Guide

VBA & macros · Updated November 3, 2026

Creating Macros in Google Sheets: A Practical Guide

To create a macro in Google Sheets, open the spreadsheet in a web browser and choose Extensions > Macros > Record macro. Perform the actions you want to repeat, choose whether cell references should be relative or absolute, then save the macro with a name. You can run it later from Extensions > Macros, or assign it a keyboard shortcut.

What a Google Sheets macro does

A macro is a saved sequence of actions that you can run again instead of repeating them manually. For example, you could record selecting a range, applying a number format, changing the text style, and adding a fill color. A later run repeats those operations in the spreadsheet.

Google Sheets implements macros using Google Apps Script, Google’s cloud-based scripting platform. When you record a macro, Sheets generates a script and associates it with the spreadsheet. That means a macro is not simply a video or a replay of mouse movements: it is a small program that performs actions through Sheets’ scripting tools.

This is different from an Excel VBA macro. Google Sheets macros use Apps Script, which is based on JavaScript, rather than VBA. Excel files with VBA do not become Google Sheets macros simply by being uploaded or opened in Sheets; a workflow that depends on VBA may need to be redesigned.

Record a macro step by step

  1. Open the spreadsheet in Google Sheets in a desktop web browser. Select the cell or range where the recorded actions should begin.
  2. Choose Extensions > Macros > Record macro.
  3. In the recording controls, choose Use relative references or Use absolute references. The choice affects how the recorded cell references behave when you run the macro later.
  4. Carry out the actions to automate. For instance, select a range, apply a format from the toolbar or a menu, and enter a formula if that is part of the routine.
  5. Choose Save in the recording controls. Give the macro a name and, if offered, choose an available shortcut number.

Keep a recording focused on one repeatable task. A short macro that formats a report header is easier to understand and maintain than a long recording that formats cells, changes sheets, edits data, and performs unrelated cleanup. If the recording includes an accidental click or an unnecessary action, it may be simpler to discard it and record again than to keep a confusing script.

Relative and absolute references

Reference mode is one of the most important decisions when recording. With relative references, actions are interpreted in relation to the cell selection used when the macro runs. This is useful when the same operation should apply to different rows or sections. For example, if you select a row of new sales figures and run a macro that formats the selected cells as currency, you can use it on other rows too.

With absolute references, the recording targets the specific cells or ranges involved in the original actions. This is useful when a macro should always format the same fixed heading or summary area. If you record a macro to apply a style to A1:D1, an absolute reference is a better fit if that exact range should be affected every time.

Before saving, ask whether you want to repeat the operation in the same location or repeat the same kind of operation wherever you select. Test the macro on a small, disposable area first. If it changes the wrong cells, revisit the reference mode and the selection you make before running it.

Run a macro and assign a shortcut

To run a saved macro, choose Extensions > Macros and select its name. This menu is the clearest option when you are learning, when you have several macros, or when a keyboard shortcut is inconvenient. If you assigned a shortcut during setup, Sheets displays the available shortcut with the macro; use the combination shown for your system. Some browser or operating-system shortcuts can conflict, so the menu remains a useful fallback.

A macro operates on the spreadsheet through the script created for it. Check which cells it targets before running it, especially if the macro changes formatting, writes values, or clears content. A macro that is harmless on a test range can produce unwanted changes when run on a large or important data area.

Macros are associated with the spreadsheet in which they are created. If you make a copy of a spreadsheet, check that the copied file contains the macro and that it still behaves as expected. Sharing a sheet does not automatically make every script action safe or appropriate for every collaborator; permissions and authorization can affect who can run a script and what it can access.

View or edit the generated Apps Script

Recording is a convenient starting point, but you can inspect the generated code by choosing Extensions > Apps Script. The editor shows the script project associated with the spreadsheet. A recorded macro is represented by a function, and you can adjust the code if you understand the changes you are making. For example, you might change the target range or add a condition that the recorder did not provide.

People who already know Apps Script can write a function directly and add it to the macro list. In the spreadsheet, use Extensions > Macros > Import macro, choose the eligible function, and confirm the import. A simple function to color the currently selected range might look like this:

function setSelectedRangeLightBlue() {
  const range = SpreadsheetApp.getActiveRange();
  if (range) {
    range.setBackground("#d9eaf7");
  }
}

This example applies a light-blue background to the active range; it does not choose a range for you. Select the intended cells before running it. Script editing is optional for ordinary recordings, but it can be useful when a task needs logic, reusable behavior, or actions the recorder does not capture well.

What macros can and cannot automate

The recorder is suited to many routine spreadsheet operations, such as applying formatting, entering formulas, and repeating supported menu or toolbar actions. However, it should not be assumed to capture every interaction or every feature in Sheets. A recording may not express a task in the reusable way you intended, particularly if the workflow depends on decisions, variable input, or external services.

Apps Script can do more than a basic recording, including using conditions and working with other Google services when the script and permissions allow it. That extra flexibility also means more responsibility: scripts can have broader effects than a formatting macro. If a task needs to inspect each row and make different choices, a hand-written script may be more appropriate than a fixed sequence of recorded steps.

For a one-time cleanup, using Sheets’ built-in tools may be faster than making a macro. For recurring work with the same predictable actions, a macro can save effort. For complex workflows involving data checks, multiple conditions, or integrations, consider whether Apps Script is the right next step rather than trying to force everything into a recording.

Permissions, safety, and troubleshooting

When a script needs authorization, Google Sheets may ask you to review and approve access before it runs. Review the requested permissions and make sure you trust the spreadsheet and its script before approving. If a file comes from someone else, inspect the macro or Apps Script project when practical, and avoid granting access to a script whose purpose or source you do not understand.

If a macro does not run as expected, check these common causes:

  • Wrong selection: Select the cells the macro expects, particularly when it uses relative references or acts on the active range.
  • Wrong reference mode: An absolute recording may keep targeting the original location, while a relative one may move with the selection.
  • Authorization not completed: Follow the authorization prompt only after checking what access the script requests.
  • Changed spreadsheet layout: Inserted, deleted, or moved rows and columns can make a fixed-range macro less useful.
  • Unsupported or altered action: Inspect the generated script in Extensions > Apps Script if the recording did not reproduce an action as expected.
  • Wrong file or account: Confirm that you opened the intended spreadsheet and are signed in to an account that can access it.

Be cautious when testing a macro that overwrites values or clears cells. Make a copy of the spreadsheet or test on sample data first. Google Sheets does not provide a dependable substitute for reviewing the effects of a script after it has run, so a backup is especially useful before experimenting with code or broad changes.

Macros versus other ways to repeat work

A macro is not always the best automation tool. Sheets formulas are better when the result should update automatically as source data changes. Features such as filters, conditional formatting, and data validation may solve recurring tasks without a script. For example, conditional formatting can highlight values that meet a rule as the data changes, while a macro would generally need to be run again to apply recorded changes.

Choose a macro when you want to repeat a sequence of user-like spreadsheet operations. Choose a formula or built-in feature when you want results to respond continuously to changing data. Choose Apps Script when you need conditional logic or broader automation that a recorded sequence cannot express cleanly. These approaches can also complement one another: a macro can apply a layout while formulas calculate the report’s results.

Unlike an Excel VBA solution, a Google Sheets macro runs in the Google Sheets and Apps Script environment. If collaborators need to work in both Excel and Sheets, check the behavior in each program rather than assuming that a macro transfers between them. The functions, scripting languages, and automation features are different.

Frequently Asked Questions

Can I create a macro in Google Sheets on my phone?
Macro recording and Apps Script editing are designed for the desktop web interface. Use Google Sheets in a web browser to create and manage macros; the mobile app does not provide the same Extensions workflow.

Do Google Sheets macros use VBA?
No. Google Sheets macros are implemented with Apps Script, which uses JavaScript-based scripting. VBA is associated with Microsoft Excel and is not the scripting language for Sheets macros.

Can I edit a macro after recording it?
Yes. Open Extensions > Apps Script to inspect or edit the script associated with the spreadsheet. Make a copy before changing code you do not understand, and test edits on sample data.

Why does my macro change the wrong cells?
Check whether the recording used relative or absolute references, and make sure the correct cells are selected before running it. A fixed reference can keep pointing to the original range, while a relative reference follows the selection.

Can I share a macro with another person?
Macros are tied to a spreadsheet and its Apps Script project, so sharing the spreadsheet can make its associated automation available to collaborators. Their ability to use it can depend on file access, script authorization, and the permissions required by the script.

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.