0
0
Google-sheetsHow-ToBeginner ยท 4 min read

How to Use Macros in Google Sheets: Simple Guide

In Google Sheets, you can use macros to automate repetitive tasks by recording your actions or writing scripts in Apps Script. Access macros via the Extensions > Macros menu to record, run, or manage them quickly.
๐Ÿ“

Syntax

Macros in Google Sheets are either recorded actions or custom scripts written in Google Apps Script. You can run them using the menu or assign shortcuts.

The basic syntax for running a macro is:

Extensions > Macros > <Macro Name>

For custom scripts, the function syntax is:

function macroName() {
  // your code here
}

Each macro function must be declared with function and have no parameters.

javascript
function myMacro() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  sheet.getRange('A1').setValue('Hello Macro!');
}
๐Ÿ’ป

Example

This example macro writes "Hello Macro!" into cell A1 of the active sheet when run. It shows how to use Apps Script to automate a simple task.

javascript
function writeHelloMacro() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  sheet.getRange('A1').setValue('Hello Macro!');
}
Output
Cell A1 in the active sheet will display: Hello Macro!
โš ๏ธ

Common Pitfalls

  • Not saving macros: After recording a macro, you must save it to use later.
  • Editing recorded macros incorrectly: Recorded macros generate Apps Script code that can be edited, but syntax errors will break them.
  • Using relative references unintentionally: Recorded macros may use relative cell references, causing unexpected results when run on different cells.
  • Permissions: Running macros that access data may require authorization; ignoring prompts can stop macros from running.
javascript
/* Wrong: Using relative reference without care */
function relativeMacro() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  sheet.getActiveCell().setValue('Test');
}

/* Right: Using absolute reference */
function absoluteMacro() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  sheet.getRange('B2').setValue('Test');
}
๐Ÿ“Š

Quick Reference

ActionHow to Do It
Record a MacroExtensions > Macros > Record macro, perform actions, then save
Run a MacroExtensions > Macros > Select macro name
Edit a MacroExtensions > Apps Script, then modify the macro function
Assign ShortcutExtensions > Macros > Manage macros > Set shortcut key
Delete a MacroExtensions > Macros > Manage macros > Delete macro
โœ…

Key Takeaways

Use Extensions > Macros menu to record, run, and manage macros easily.
Macros can be recorded or written as Apps Script functions with no parameters.
Always save your macro after recording to use it later.
Be careful with relative cell references in recorded macros to avoid unexpected results.
Edit macros in Apps Script to customize or fix errors.