VBA Code Writing With AI: Build and Check Excel Macros

Use AI to draft Excel macros from a clear workbook description, then check the code and its results on a copy. Keep the requirements and debugging lessons for the next session.

Updated 5 min read

Use AI for VBA code writing by describing the workbook, requesting one small macro, reviewing its operations, and testing it on a disposable copy. A convincing explanation is not evidence that the macro changed the correct cells. fenbs can keep the nonsensitive requirements and debugging lessons available when you return in a new chat.

VBA, Visual Basic for Applications, is the programming language used to automate actions in Office. Microsoft recommends considering built-in features before writing code in its getting-started guide (opens in a new tab). A formula, table, or existing command may solve your problem with less maintenance.

What is a reliable workflow for your first macro?

  1. Define one observable result. For example, copy approved rows into a new report sheet while leaving every source value unchanged.
  2. Create a test workbook with invented data. Preserve the real sheet names, headers, formulas, and awkward cases that matter to the operation.
  3. Ask the assistant to describe its proposed steps before producing code. Resolve questions about blanks, duplicate rows, and existing output first.
  4. Review the macro, compile the VBA project in the editor, and step through a small test before running it over the whole sample.
  5. Compare actual results with a result you calculated independently. Record failures, revise one cause at a time, and rerun the relevant checks.

Start with a macro that reads data and writes to a separate destination. Leave deletion, file replacement, and sending messages out of the first version. This makes the result easier to inspect and gives you a clear boundary for reviewing any later expansion.

Say your sample has eight rows and three should appear in the report. Write down those three row identifiers before running anything. Count matching rows afterward, inspect their values, and check that the other five were excluded for the right reason. Successful execution alone does not answer those questions.

What workbook details should you give the AI?

Describe structure rather than uploading a production workbook by default. Include the Excel environment you will use, workbook format, sheet names, table names, headers, data types, and the destination. State whether the macro lives inside the workbook it edits or another workbook. Those distinctions affect which objects the code should address.

  • Inputs: identify the exact table or range and explain whether formulas, hidden rows, and filtered rows count.
  • Rules: define matching text, date interpretation, blanks, duplicates, and what constitutes a valid row.
  • Outputs: say whether to replace a previous report, append rows, or stop when output already exists.
  • Boundaries: list sheets and files that must remain unchanged, and forbid network access or external applications unless the task needs them.

Use invented customer names and amounts while preserving tricky formats. A date stored as text is a different test from a real date displayed in a particular style. An empty string returned by a formula may need a different rule from a truly empty cell. Explain your intended treatment instead of asking the model to guess.

This is where an AI brain helps: the durable information is the workbook contract and its unresolved questions, rather than a transcript of every code suggestion.

How should you ask for reviewable VBA?

Request explicit workbook and worksheet references, short procedures, declared variables, and a plain explanation of what the macro can change. Microsoft documents that Option Explicit (opens in a new tab) requires variable declarations and catches undeclared names at compile time. It helps catch typos; it does not prove the business logic.

A prompt for a small report macro
Draft Excel VBA for my test workbook.
Source: ThisWorkbook, sheet Orders, table OrdersTable.
Headers: OrderID, Status, Amount.
Copy rows whose Status is exactly Approved to a new ReviewReport sheet.
Do not edit the source, delete sheets, save files, or contact other apps.
Stop with a clear message if ReviewReport already exists.
Use Option Explicit and explicit object references.
First list assumptions. Then provide code and a test checklist.

Ask the assistant to identify operations that depend on the active selection, change application settings, overwrite formulas, or access files. If it disables events or changes calculation mode, ask how those settings are restored after both success and failure. Review the answer against the code rather than accepting the commentary separately.

Our guide to reviewing AI-generated code explains the wider review habit. For a workbook, keep the review focused on the exact cells and side effects this macro can reach.

Which tests catch plausible but incorrect results?

Test an ordinary input, no matching rows, all matching rows, missing headers, blank cells, duplicate identifiers, and output that already exists. Add a second run: it should follow the replacement or append rule you agreed. Also test with a different worksheet selected so accidental reliance on the current sheet becomes visible.

Keep before-and-after copies. Compare source formulas, formats, row counts, and totals where relevant. A report total can match while individual rows are wrong, so check identities and values together. Confirm that the workbook is still usable after a failed run, including any application settings the macro changed.

Be suspicious of broad error suppression. Microsoft explains that On Error Resume Next (opens in a new tab) continues after a runtime error. That can be intentional for a specific checked operation, but suppressing errors throughout a procedure can leave a partial report looking complete.

Save a useful debugging lesson in fenbs: what input failed, what the code assumed, and which check now prevents the mistake. Keep the example synthetic and describe the observed failure precisely.

How do you hand off a macro that passed?

Keep the reviewed code with the workbook or your normal code repository. Add a short operating note: its purpose, where to run it, required headers, expected output, and recovery steps. Record the Excel environment in which you tested it and any environment you have not checked. A test on your computer does not establish compatibility everywhere.

Have the workbook owner confirm that the output answers the business question before introducing it into routine use. For broader assistant choices, see AI code generators; configuration files have their own workflow in AI for writing YAML. More practical workflows live in AI at work.

How fenbs helps

Keep the workbook rules between debugging sessions

Create a project for the automation and save its nonsensitive input rules, open questions, and test results. Record repeated failures as Lessons learned so the next connected assistant can read them before suggesting another fix.

Create a free account

Free for one person · No credit card · Works with Claude, ChatGPT, Cursor and more

How to do this with fenbs

  1. Create a free account and add a project for the workbook automation. Use a short description of its intended result.
  2. Add a task for the macro and put the implementation steps in its plan. Record approved input and output rules in Decisions and rules.
  3. Connect a supported assistant through Integrations and ask it to read the project context before drafting or debugging.
  4. Record the checks you actually ran in the task, add any reusable lesson, and save Where we left off with the next step and remaining uncertainty.

fenbs does not execute VBA or inspect your Excel workbook. For a one-off macro, a clear local note may be enough; fenbs becomes useful when the rules and unfinished debugging must survive several sessions or assistants.

Questions people ask.

Do I need to know VBA before using an AI assistant?

You need enough understanding to review the operations and judge the result, or someone qualified to do that review. Start with a small task and ask for explanations of unfamiliar statements before running the code.

Should I paste my real workbook into a chat?

Prefer a synthetic workbook or a description of its structure when possible. Follow your organization’s rules for sharing data, and remove confidential information from examples and error reports.

What should I send the AI when a macro fails?

Provide the exact error, the failing line, a small invented input that reproduces it, and the expected result. Include relevant workbook structure and explain which checks have already passed.

Can I trust a macro after it runs once without an error?

No. It can run successfully while selecting the wrong rows or overwriting the wrong cells. Compare independently expected results, check side effects, and test failure cases and repeat runs.

Start with one thing.

Free for one person · No credit card · Works with Claude, ChatGPT, Cursor and more

There is nothing to set up first. Write one line and you’ve started.