A neural network for Excel macros: VBA code from a task description
How to phrase a task for the model to get a working VBA macro, how to test it on a copy of the file and what to do about Run-time errors.
5 min · · автоматизация · development
A neural network for writing Excel macros saves time where you know what result you want but do not remember VBA syntax. The model writes code from a description and you run it in the Visual Basic editor. The main condition for success is not the model but how you phrase the task and how carefully you test: a macro changes data, and its effect cannot be reversed with the usual Undo button. Below are the workflow, an example of a finished macro, typical errors and how to call the model from a script.
How to describe the task
The model cannot see your workbook, so everything obvious to you has to be written out. The description should include:
- Data structure. The sheet name, which row holds headers, which columns exist and what is in them.
- The action. What to do: delete, calculate, copy, colour.
- Boundaries. From which row, to which, by which column to find the last row.
- Edge cases. What to do with empty cells, text instead of numbers, an empty sheet.
- The result. Where to put the output: the same sheet, a new one, a message.
- Version and system. Excel for Windows or Mac: some VBA objects are unavailable on Mac.
A bad brief: "Delete the extra stuff." A good one: "Sheet 'Продажи', headers in row 1, data from A2. Delete fully empty rows (all cells empty). Go from the bottom up. The macro is for Excel on Windows; use Option Explicit and comments in English."
Do not put real data in the request: headers and two or three made-up rows are enough.
An example of the result
Here is the macro that comes out of the brief above:
Option Explicit
Sub DeleteEmptyRows()
Dim ws As Worksheet
Dim lastRow As Long, r As Long
' The data sheet
Set ws = ThisWorkbook.Worksheets("Продажи")
' Last row across the whole used range
lastRow = ws.UsedRange.Rows.Count + ws.UsedRange.Row - 1
Application.ScreenUpdating = False
' Bottom-up so that shifting rows does not break the count
For r = lastRow To 2 Step -1
If Application.WorksheetFunction.CountA(ws.Rows(r)) = 0 Then
ws.Rows(r).Delete
End If
Next r
Application.ScreenUpdating = True
End SubNote two decisions worth checking in any macro. Rows are walked from the bottom up: deleting a row shifts the rows below it upward, and going top-down would skip some rows. The last row comes from UsedRange, not from column A: if we looked for the last filled cell in a single column, rows holding data only in other columns, located below it, would go unprocessed. This is a typical hidden mistake in generated code, so ask the model: "which column do you use to find the last row and what happens if it is empty?"
How to test a macro safely
A macro runs immediately and usually clears the undo stack. Testing has to happen beforehand:
- Work on a copy of the file. Save the workbook under another name and run the macro on it. This is the main rule.
- Read the code before running. Calls to
Kill,Shell,CreateObject, network access andWorkbooks.Openon unexpected paths are especially worrying. If you do not understand a line, ask the model what it does. - Run it step by step. In the VBA editor press F8 on a small test workbook and watch variable values.
- Check edge cases. An empty sheet, a single data row, cells with formulas, hidden rows, merged cells.
- Save as .xlsm. Macros are not saved in an ordinary .xlsx.
- Do not enable macros from files you did not get from a trusted source. This applies to other people's workbooks, not to code you read yourself.
What to do about run-time errors
When a macro crashes, Excel shows a "Run-time error" window with a number. Copy the number, the text and the line highlighted after you click Debug, and send them to the model with the code. Common cases:
| Error | What it usually means |
|---|---|
| Run-time error 9: Subscript out of range | No sheet or workbook with that name; check the name "Продажи" |
| Run-time error 13: Type mismatch | A cell has text where a number was expected, or a #N/A error |
| Run-time error 1004 | A general application error: protected sheet, wrong range, merged cells |
| Run-time error 424: Object required | A variable is not assigned: Set was forgotten |
When asking for a fix, give full context: the code, error number and text, the line, variable values if you know them. "It doesn't work" gives nothing.
Generating a macro from a script
If you need macros regularly, the brief can move into a script. A Python example with the OpenAI SDK:
from openai import OpenAI
client = OpenAI(base_url="https://api.zukko.pro/v1", api_key="fetch_your_key")
task = """Sheet "Продажи", headers in row 1, data from A2.
Columns: A date, B manager, C amount.
I need an Excel macro for Windows: on a new sheet "Итоги" total the amounts per manager.
Use Option Explicit, comments in English, handle an empty sheet."""
resp = client.chat.completions.create(
model="claude-sonnet-5-5",
messages=[
{"role": "system", "content": "You write VBA macros for Excel. Reply with code only in a vba block, no explanations."},
{"role": "user", "content": task},
],
max_tokens=1500,
)
print(resp.choices[0].message.content)Choose the model by your own tests. For short macros inexpensive options are usually enough; for complex ones try stronger models; the approach is described in the article which model writes Python better, and the principle is the same. The request format for /v1/chat/completions is in the documentation.
What it costs
Prices as of October 18, 2026. Say a macro request takes 500 input tokens and the answer 1,000 tokens:
claude-sonnet-5-5(0.4 / 2 per 1M): 0.0005 x 0.4 + 0.001 x 2 = 0.0002 + 0.002 = $0.0022gpt-6.1-sol(0.27 / 1.32): 0.0005 x 0.27 + 0.001 x 1.32 = 0.000135 + 0.00132 = about $0.0015
A thousand such macros cost roughly $2.2 and $1.5. The main spend of time goes into checking, not tokens. Prices change; the current list is at GET https://api.zukko.pro/v1/models.
If you need formulas rather than macros, see a neural network for Excel formulas; for browser spreadsheets there are Google Sheets scripts. You create a key in the dashboard.
FAQ
Can I trust a macro written by a neural network?
Only after checking. Read the code, run it on a copy of the file and on a small sample, look at edge cases. A model can write plausible but wrong code.
Do I need to know VBA to use a neural network?
Basic understanding helps you read the code and find a problem, but you can start without it: ask the model to add comments and explain every step.
What if the macro works on my PC but not on a colleague's?
Most likely the cause is a difference in version or system: objects like Scripting.Dictionary are unavailable in Excel for Mac. State your system and Excel version in the request.
Does this work with Google Sheets?
No, VBA belongs to Excel. For Google Sheets people use Apps Script; you can ask the model to write that too by stating it in the task.
zukko
Pay 25×
less
for Claude Code & ChatGPT
One fetch_* key and base URL api.zukko.pro — for Cursor, Claude Code, Codex, and any SDK.
Related articles
- Translating subtitles with a neural network: SRT without broken timecodesThe main risk when translating SRT with a neural network is a broken file structure. A scheme where timecodes never reach the model, and a Python script with validation.
- Editing texts with a neural network: proofreading, style and cuttingDifferent kinds of editing need different prompts. Wording for proofreading, style and cutting, how to get edits with explanations and how not to lose the author's voice.
- A neural network for writing scripts: Bash, PowerShell and PythonA model writes scripts for routine fast, but running them blindly is risky. How to state the task, demand a safe dry-run mode and check the result before launch.
- Dify in Docker: installation, first-run setup and connecting modelsInstall Dify with Docker Compose, create the admin account and connect Claude, GPT and DeepSeek models on one key. Plus what to do when containers will not start.