Write a Commented VBA Macro to Automate a Repetitive Excel Task
Get a commented, beginner-safe VBA macro built around your sheet names, ranges and trigger, with install steps, test advice and a backup warning.
You getBackup warning, what the macro does and the full code in one code block
Prompt
You are an Excel VBA developer who writes safe, readable macros for non-programmers.
Write a VBA macro that automates a repetitive task in my workbook. Before writing code, ask me these questions in one message and wait for my answers:
1. What task do you repeat, step by step, as you do it by hand?
2. What are the exact sheet names and the ranges or columns involved? Does the number of rows change?
3. How should it run: a button, a keyboard shortcut, on workbook open, or when a cell changes?
4. Which Excel version and platform (Windows or Mac)?
Rules:
- Comment every block in plain English.
- Find the last used row dynamically; never hard-code row counts.
- Use Option Explicit, declare every variable, and add error handling that shows a clear message instead of crashing.
- Turn screen updating off during the run and always restore it, even after an error.
- Ask for confirmation before deleting or overwriting any data.
Output format:
1. Backup warning: one line telling me to save a copy first, because macro changes cannot be undone with Ctrl+Z.
2. What the macro does: 3-5 bullets.
3. The full code in one code block.
4. Install steps: numbered, from opening the VBA editor to saving as .xlsm and setting up the trigger.
5. How to test it safely.
End with one line listing your assumptions and anything I should confirm.Example output
Tips and common edits
Why this works
- Asking for sheet names, ranges and trigger first means the code runs on your real workbook instead of a generic example.
- Rules for dynamic last row, Option Explicit and error handling prevent the most common macro failures.
- The backup warning and confirmation prompt protect data, since VBA changes cannot be undone.
Use when you repeat the same copy, filter or cleanup steps every week. Have your sheet names and column letters ready. Works in ChatGPT, Claude or Gemini; paste the code into Excel's VBA editor.
Common edits
- Ask for a Google Sheets Apps Script version instead of VBA.
- Add a rule to log each run's date and row count to a separate sheet.
Prompt details
- By
- Prompts-GPT editors
- Published
Did this prompt work?
Keep exploring
More like this
Fix an Excel or Google Sheets Formula That Returns an Error or Wrong Result
Get a ranked list of the likely reasons your formula fails, a quick check for each, and a corrected formula you can paste straight into your sheet.
Write a Google Apps Script to Automate a Sheets Task with Dry-Run Mode
Get a working Google Apps Script for your Sheets task, with trigger setup, the permission screens you will see, and a dry-run mode that logs changes first.
Write an Excel or Google Sheets Formula from a Plain-English Description
Get a ready-to-paste Excel or Google Sheets formula built for your version and columns, with a step-by-step walkthrough and one test case to check it.
Build a 13-Week Cash Flow Forecast in Excel with Low-Balance Alerts
You get a 13-week cash flow sheet layout with committed and expected cash split apart, exact running-balance formulas, and red flags for weeks below your minimum.
Build a Break-Even and Pricing Calculator Spreadsheet in Excel
You get an Excel layout that separates fixed and variable costs, formulas for break-even and margin at three prices, and a ranking of which inputs move the result most.
Build an Employee Shift Schedule and Hours Tracker with Overtime in Excel
You get a ready-to-build spreadsheet layout with formulas for overnight shifts, unpaid breaks, weekly hours per employee and highlighted overtime.