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:

  1. Data structure. The sheet name, which row holds headers, which columns exist and what is in them.
  2. The action. What to do: delete, calculate, copy, colour.
  3. Boundaries. From which row, to which, by which column to find the last row.
  4. Edge cases. What to do with empty cells, text instead of numbers, an empty sheet.
  5. The result. Where to put the output: the same sheet, a new one, a message.
  6. 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:

bash
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 Sub

Note 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:

  1. Work on a copy of the file. Save the workbook under another name and run the macro on it. This is the main rule.
  2. Read the code before running. Calls to Kill, Shell, CreateObject, network access and Workbooks.Open on unexpected paths are especially worrying. If you do not understand a line, ask the model what it does.
  3. Run it step by step. In the VBA editor press F8 on a small test workbook and watch variable values.
  4. Check edge cases. An empty sheet, a single data row, cells with formulas, hidden rows, merged cells.
  5. Save as .xlsm. Macros are not saved in an ordinary .xlsx.
  6. 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:

ErrorWhat it usually means
Run-time error 9: Subscript out of rangeNo sheet or workbook with that name; check the name "Продажи"
Run-time error 13: Type mismatchA cell has text where a number was expected, or a #N/A error
Run-time error 1004A general application error: protected sheet, wrong range, merged cells
Run-time error 424: Object requiredA 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:

bash
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.0022
  • gpt-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.

Get API keyDocumentation

api.zukko.pro · fetch_*

A neural network for writing Excel macros and VBA code · Zukko