Excel VBA code generator
Describe the automation in plain English. You get a complete Sub with every variable declared and every object reference qualified, plus notes on what will trip it up.
How to generate and run an Excel VBA macro
- 1
Describe the automation step by step
Name the sheets, the columns and the trigger. "On the Orders sheet, for every row where column F is Overdue, copy the whole row to the Followup sheet" gives the generator everything it needs.
- 2
Open the VBA editor
Alt+F11 on Windows, or Tools › Macro › Visual Basic Editor on Mac. Insert › Module gives you somewhere to paste. The macro will not run from a worksheet cell — it has to live in a module.
- 3
Paste and press F5
Run it on a copy of the workbook the first time. VBA has no undo: a macro that deletes the wrong rows has deleted them, and Ctrl+Z will not bring them back.
- 4
Save as .xlsm
A normal .xlsx file silently discards macros when it saves. Excel warns you once; if you click past it your code is gone.
What the generated macros assume
Every macro comes with Option Explicit above it and every variable declared. This is not style — undeclared variables in VBA default to Variant and silently swallow typos, so a misspelled sheet name becomes an empty value rather than an error you can see.
Object references are qualified: ThisWorkbook.Worksheets("Orders").Range(...) rather than Range(...). An unqualified reference operates on whatever sheet happens to be active when the macro runs, which works perfectly during testing and fails the first time a colleague runs it from a different tab.
Loops find the last used row rather than hard-coding one. A macro written against 500 rows of data breaks the month someone adds row 501, and that break is silent — it just processes less than it should.
VBA or Office Scripts or Apps Script
VBA runs in desktop Excel on Windows and Mac, has full access to the object model, and is not going anywhere despite regular predictions. It does not run in Excel for the web.
Office Scripts is the TypeScript-based replacement that does run on the web, and pairs with Power Automate. It has a narrower object model and no access to anything outside the workbook.
Apps Script is the Google Sheets equivalent and is a different language again — JavaScript, with its own Spreadsheet service. Set the switch above to Apps Script and you get that instead of VBA.
Running macros safely
Macros are code, and code you did not write deserves reading before it runs. Generated VBA is no different: skim it for Delete, ClearContents and SaveAs before pressing F5.
Test on a copy. This is the single habit that separates a bad afternoon from a lost workbook, because VBA operations do not enter the undo stack.
If Excel blocks the file entirely, that is Mark of the Web, not a bug — files downloaded from the internet are blocked from running macros. Right-click the file, Properties, and tick Unblock, and only do it for files you trust.
VBA objects you will meet most often
| Expression | What it is | Why it matters |
|---|---|---|
| ThisWorkbook | The workbook the code lives in | Safer than ActiveWorkbook, which follows the user's focus |
| Worksheets("Name") | One sheet by name | Breaks if the tab is renamed; use the CodeName for stability |
| Range("A1:C10") | A block of cells | Always qualify it with a sheet, or it uses whatever is active |
| Cells(row, col) | One cell by number | The way to address cells inside a loop |
| Cells(Rows.Count, 1).End(xlUp).Row | Last used row in column A | Use this instead of hard-coding a row count |
| Range.Offset(r, c) | Move relative to a range | Handy, but Cells() is usually clearer to read later |
| Application.ScreenUpdating | Redraw toggle | Set False at the start of a long loop, True in the exit path |
| Application.WorksheetFunction | Call a sheet function | Errors raise a runtime error rather than returning #N/A |
| Worksheet_Change(ByVal Target) | Fires when a cell changes | Lives in the sheet's module, not a standard module |
| On Error GoTo label | Error handling | Always restore ScreenUpdating in the handler as well |
VBA generator FAQ
- Do I need to install anything?
- No. VBA is built into desktop Excel on Windows and Mac. You open the editor with Alt+F11 and paste. There is nothing to download from this site.
- Why does my macro disappear when I save?
- Because the file is a .xlsx, which cannot store macros. Save as .xlsm — Excel warns you when you are about to lose the code, but the warning is easy to click past.
- Can it write Google Apps Script instead?
- Yes. Switch the toggle to Apps Script and you get JavaScript against the Spreadsheet service rather than VBA. The object models are completely different, so set the switch before you generate rather than translating after.
- Will the macro work in Excel for the web?
- No. VBA does not run in Excel for the web at all. If that is your target you want Office Scripts, which is a different language — say so in your description and you will get TypeScript instead.
- Is generated VBA safe to run?
- Read it first, and run it on a copy. The macros here are written to be conservative, but VBA has no undo, so a mistake in a delete loop is permanent. Skimming for Delete and ClearContents takes ten seconds.
- Can it handle events, like running on cell change?
- Yes — ask for a Worksheet_Change event and say which range should trigger it. Event macros go in the sheet's code module rather than a standard module, and the answer will say so.