Macro
A recorded or written sequence of commands and actions that can be saved and replayed to automate repetitive tasks in spreadsheet applications.
Examples
## Recording vs Writing Macros
### Recording (Easier)
Pros: No coding required Cons: Limited flexibility, can be inefficient
### Writing (More Powerful) Write code directly in VBA or Apps Script
Pros: Complete control, efficient, conditional logic Cons: Requires programming knowledge
## Recording a Macro in Excel
1. View → Macros → Record Macro 2. Name your macro 3. Choose shortcut key (optional) 4. Perform your actions 5. View → Macros → Stop Recording
## Recording a Macro in Google Sheets
1. Extensions → Macros → Record macro 2. Choose relative or absolute references 3. Perform your actions 4. Click Save 5. Name your macro
## Example: VBA Macro
Sub FormatReport()
' Format headers
Range("A1:E1").Font.Bold = True
Range("A1:E1").Interior.Color = RGB(0, 112, 192)
Range("A1:E1").Font.Color = RGB(255, 255, 255) ' Auto-fit columns
Columns("A:E").AutoFit
' Add borders
Range("A1").CurrentRegion.Borders.LineStyle = xlContinuous
End Sub
## Example: Apps Script
function formatReport() {
const sheet = SpreadsheetApp.getActiveSheet(); // Format headers
const headers = sheet.getRange("A1:E1");
headers.setFontWeight("bold");
headers.setBackground("#0070C0");
headers.setFontColor("#FFFFFF");
// Auto-resize columns
sheet.autoResizeColumns(1, 5);
}
## Running Macros
### Excel
### Google Sheets
## Security Considerations
### Excel
### Google Sheets
## Best Practices
1. Comment your code: Explain what each section does
ProcessMonthlySales not Macro1
3. Handle errors: Add error handling for robustness
4. Test thoroughly: Check edge cases and invalid inputs
5. Back up: Save before running new macros
6. Modularize: Break complex macros into smaller procedures## When to Use Macros
Good candidates for automation:
Not ideal for: