Google Sheets Macros: Examples and How to Use Them

VBA & macros · Updated November 21, 2026

Google Sheets macros are saved actions you can replay to automate repetitive work, such as formatting a report, cleaning a copied list, or preparing a table for review. To make one in a browser, choose Extensions > Macros > Record macro, perform the steps, and save; you can also edit the resulting Google Apps Script for more control.

What a Google Sheets macro does

A macro captures a sequence of actions in a spreadsheet and saves them as a function in a Google Apps Script project associated with that spreadsheet. Running the macro repeats those actions. This makes macros useful for predictable tasks that would otherwise require the same series of clicks each time.

For example, a monthly report might arrive with an unformatted header and inconsistent column widths. A macro can apply a header style, freeze the top row, and resize columns in one run. A different macro could format dates and currency in a standard way before a sheet is shared.

Macros are not the same as formulas. A formula calculates a result in a cell and can update when its inputs change; a macro performs actions when someone runs it. Nor is a Sheets macro a recording of mouse movements: the recorder translates supported spreadsheet actions into Apps Script commands. The saved script can be inspected and edited.

Practical Google Sheets macro examples

These are common tasks that work well as macros when the relevant sheet layout stays consistent. A macro may need adjustment if the table moves or gains columns.

  • Format a report header: bold the first row, add a fill color, align labels, and freeze the row so headings remain visible while scrolling.
  • Standardize number formats: apply a date format to a date column, a currency format to a sales column, or a consistent number of decimal places to measurements.
  • Prepare a print or review view: resize columns, set text wrapping, and apply a consistent font or alignment before colleagues inspect the data.
  • Clean a recurring import: remove unwanted formatting, set predictable column widths, or apply the same formatting to a pasted data range. Check the selected range carefully before recording destructive actions.
  • Set up a weekly tracker: add borders, style labels, and freeze the rows or columns that contain identifiers. A macro can prepare the template, though it will not automatically know which new records belong in it.

These examples automate presentation and routine editing; they do not automatically make unreliable source data correct. If a task depends on conditions, changing row counts, or decisions such as whether a value is valid, a formula or a more deliberately written script may be safer than a simple recording.

How to record a macro in a browser

  1. Open the Google Sheets file and, if possible, make a copy or test on sample data first.
  2. Choose Extensions > Macros > Record macro.
  3. Choose whether the macro should use absolute references or relative references. This choice affects which cells the recorded actions target when replayed.
  4. Carry out the spreadsheet actions in the order you want them repeated. Select only the cells the macro should change.
  5. Choose Save, give the macro a useful name, and save the recording.
  6. To replay it, use Extensions > Macros and select the saved macro. If Sheets asks for authorization, review the request before approving it.

Recording is easiest when you first write down the steps and know the intended starting cell. A short, focused macro is easier to verify than one that combines unrelated cleanup, formatting, and sorting operations.

Absolute and relative references

Reference mode is one of the most important choices during recording. With an absolute reference, recorded actions target the same cells each time. If a macro formats A1:D1 using absolute references, running it again will target that range even if the active cell is somewhere else. That can suit a fixed report template.

With relative references, the recorded actions are positioned in relation to the active cell or selection used when the macro runs. This can make a macro reusable on different rows or sections. For instance, a formatting sequence recorded relative to the current cell may be applied to another block by selecting its starting cell first. The exact result depends on the actions recorded, so test the macro on a copy and confirm which cells change.

Neither mode is universally better. Use absolute references when the target range is stable and the macro should always act there. Choose relative references when you deliberately want to repeat the same pattern around a new selection. Be cautious with macros that rely on the active sheet or current selection: running one from an unexpected location can produce an unexpected result.

Inspecting and editing the generated script

Every recorded macro is backed by Apps Script. To see its code, choose Extensions > Apps Script. The editor is useful for checking which ranges a recording affects or making a small, consistent formatting routine easier to understand. The following example formats a fixed header range; it is illustrative code, not a macro that Sheets records automatically.

function formatSalesHeader() {
  const sheet = SpreadsheetApp.getActiveSheet();
  const header = sheet.getRange('A1:D1');

  header.setFontWeight('bold');
  header.setBackground('#d9ead3');
  sheet.setFrozenRows(1);
  sheet.autoResizeColumns(1, 4);
}

This function acts on the active sheet, formats cells A1:D1, freezes the first row, and resizes the first four columns. It assumes the headings are in that fixed range. If another sheet has headings in A3:D3, the function will not adapt by itself. A developer can change the range or add logic, but should test any edits before relying on them.

Apps Script is JavaScript-based, while Excel macros are commonly written in VBA. A VBA macro cannot simply be pasted into the Sheets script editor, and Apps Script code does not run as an Excel VBA macro. The two platforms have different APIs and permissions.

Running, naming, and sharing macros

Use Extensions > Macros to run a saved macro. In Extensions > Macros > Manage macros, you can manage recorded macros and, where available, assign keyboard shortcuts. Give each macro a name that describes its result, such as “Format weekly report,” rather than a vague label like “Macro 2.” This makes the menu more useful as the list grows.

A macro belongs to the spreadsheet in which it was created, rather than being a universal setting that automatically appears in every file. If colleagues need to use it, share the spreadsheet and explain what the macro changes. A person running a script may be asked to authorize it, and authorization depends on the code and the account’s access. A copied spreadsheet may also carry its associated script, so check that the macro is present and behaves as expected in the copy.

When a macro is the wrong tool

Macros are best for repeated, clearly defined actions. Other Sheets features are often better for work that should update continuously or adapt to changing data:

  • Use formulas when you need calculated results that recalculate as source cells change.
  • Use conditional formatting when colors should respond automatically to values or rules. In Sheets, open Format > Conditional formatting.
  • Use a filter or filter view when the goal is to show selected records, rather than repeat a formatting routine.
  • Use Apps Script directly when the task needs branching logic, custom menus, or handling of data that changes shape. More code also means more responsibility to review and maintain it.

A macro is not a timer or a data-cleaning guarantee. Recording a routine does not automatically make it run when a new row is added, and it will not infer what a user intended. For unattended or event-based automation, investigate Apps Script triggers and their authorization requirements rather than assuming a recorded macro runs by itself.

Common problems and safe use

If a macro is missing from the menu, confirm that you opened the correct spreadsheet and that the macro was saved. If it runs but changes the wrong cells, check its reference mode, active sheet, and selected range. If the layout has changed since recording, revise the recording or script and test it against the new layout.

When a macro fails or prompts for access, read the error or authorization screen instead of repeatedly approving requests. A script can have access to spreadsheet data and, depending on its code, other Google services. Use macros from trusted files, inspect unfamiliar code in Extensions > Apps Script, and avoid granting access when you do not understand why it is requested. Keep an unchanged copy of important data before testing a routine that deletes, overwrites, or rearranges cells.

Macros are designed for Google Sheets in a web browser. The Sheets mobile app is not a practical place to record or manage macros; use a computer browser for the recording and script-editing workflow. Menu names and available account controls can vary as Google updates its interface or applies organizational policies.

Frequently Asked Questions

Are Google Sheets macros the same as Excel macros?

No. Sheets macros are implemented with Google Apps Script, while Excel macros are commonly implemented with VBA. Their code and spreadsheet APIs differ, so a macro from one program generally needs to be rewritten for the other.

Can I run a Google Sheets macro automatically?

A recorded macro normally runs when a user selects it from the menu or uses its assigned shortcut. For automation based on a schedule or an event, Apps Script triggers may be relevant, but they are separate from simply recording a macro and can require additional permissions.

Can a macro work on different ranges?

It can, especially when recorded with relative references, but the result depends on the actions and selection used. A macro that refers to fixed cell addresses will continue targeting those addresses unless its script is changed. Test it on each intended layout.

Where do I edit a recorded macro?

Open Extensions > Apps Script to view and edit the spreadsheet’s script project. Read the function and range references before changing them, then run a controlled test to verify the result.

Why does Google Sheets ask me to authorize a macro?

Macros run as Apps Script, and a script may need permission to access the spreadsheet or other services. Review the requested access and make sure you trust the file and its code before authorizing it.

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.