Writing Spreadsheet Formulas With LLMs in October 2026: Name Your App and Layout, Use Exact-Match Lookups, and Test Against Known Answers
Asking a chat model for a spreadsheet formula is one of the most common everyday uses of generative AI. You describe what you want, such as "sum sales for the West region in Q3", and you get back something that looks right. The trouble is that spreadsheet formulas fail quietly. A wrong range or a lookup that matches the wrong row doesn't throw an error. It just puts a plausible number in a cell, and that number ends up in a report. This guide covers how to get formulas from an LLM that are correct for your app, your data, and your edge cases.
A SUM formula in LibreOffice Calc. Image: Arlo Barnes, CC0, via Wikimedia Commons.
Why LLM-written formulas go wrong
Most bad formulas from a model come from a handful of predictable gaps:
- The wrong spreadsheet app. Excel, Google Sheets, LibreOffice Calc, and Apple Numbers share a lot of syntax, but not all of it. Google Sheets has
QUERY,IMPORTRANGE, andREGEXMATCH; Excel doesn't have those under the same names. Newer Excel functions likeXLOOKUP,FILTER,LET, andLAMBDAaren't available in older perpetual-license versions. - Guessed layout. If you don't say where the data lives, the model invents columns. It may assume headers in row 1 and data in A:D when your sheet has a title block and headers in row 4.
- Locale differences. In many European locales, the argument separator is a semicolon rather than a comma, and the decimal separator is a comma. A formula that works in a US-English sheet may be rejected in a German one.
- Silent lookup mistakes.
VLOOKUPwithout the last argument set toFALSEdoes an approximate match, which on unsorted data can return a wrong row without any warning. - Happy-path thinking. Blank cells, text that looks like numbers, duplicate keys, and dates stored as text are where formulas break, and models rarely handle them unless asked.
Step 1: Tell the model exactly what it's working with
The single biggest improvement is a better prompt. Include four things every time:
- The app and version. "Google Sheets" or "Excel for Microsoft 365" or "Excel 2016". This decides which functions are fair game.
- The layout. Sheet names, header row, which columns hold what, and how many rows there are or whether the range will grow.
- A few real sample rows. Paste five to ten rows, including the messy ones: a blank, a duplicate, a value stored as text. Remove anything sensitive first.
- The expected result for a couple of cases. "For the West region in Q3, the answer should be 48,200." This gives you, and the model, a check.
A prompt that works well looks like this:
App: Google Sheets.
Sheet "Orders": headers in row 1, data in rows 2-5000 (grows weekly).
A = Order date (real dates), B = Region (text), C = Amount (number, some blanks).
Sheet "Summary": B2 holds a region name, C2 holds a quarter number (1-4).
I need a formula for D2 that sums Amount for the region in B2
and the quarter in C2 of the year 2026. Blanks in Amount count as 0.
Sample rows:
2026-07-03 | West | 1200
2026-08-15 | west |
2026-09-30 | East | 800
Expected: Region "West", quarter 3 should include the first row only
(row 2 has a blank amount).
Explain each part of the formula, and tell me what it does if
Region has inconsistent capitalization.
Notice the last line. Asking the model to explain the formula and describe edge-case behavior surfaces assumptions you can check before you trust it.
Step 2: Prefer readable formulas over clever ones
Models sometimes produce deeply nested formulas that technically work but no one can maintain. Ask for readability instead. Useful patterns:
- Use the right purpose-built function. For the example above,
SUMIFSis clearer than an array trick:
Note that the=SUMIFS(Orders!C:C, Orders!B:B, B2, Orders!A:A, ">="&DATE(2026, 3*C2-2, 1), Orders!A:A, "<"&EDATE(DATE(2026, 3*C2-2, 1), 3))SUMIFStext criteria match case-insensitively in both Excel and Google Sheets, so "West" and "west" are both counted. That's often what you want, but you should know it. - Use
LETwhen available. In Excel for Microsoft 365 and in Google Sheets,LETlets you name intermediate values so a long formula reads top to bottom. Ask the model for aLETversion if the formula repeats the same expression more than once. - Use exact-match lookups. If you need a lookup, ask for
XLOOKUP(where supported) orINDEX/MATCHwith a match type of 0. If the model hands youVLOOKUP, check that the last argument isFALSE. - Handle "not found" on purpose. Wrapping everything in
IFERROR(..., "")hides real problems along with expected ones. PreferXLOOKUP's own "if not found" argument orIFNA, which only catches missing matches, so other errors still show up.
Step 3: Test the formula against cases you already know
Never judge a formula by whether it returns a number. Check it against answers you can verify independently.
- Build a tiny test sheet. Copy ten or twenty rows into a scratch tab, including edge cases: a blank amount, a text value like
"1,200", a duplicate key, a date at the very edge of the range (the first and last day of the quarter). - Work out the right answer by hand or with a filter and the status-bar sum. Compare it with the formula's result.
- Change one input at a time. Switch the region, the quarter, or blank out a cell, and confirm the result moves the way you expect.
- Check the boundaries. Off-by-one errors in date ranges are common:
"<="versus"<"on an end date, or an end date that drops the last day because the data includes times.
If the formula fails a case, paste the failing input and the wrong output back to the model and ask it to fix that specific case. That's far more effective than "it doesn't work".
An abacus in use at a kimono shop in Otaru. Image: Syced, CC0, via Wikimedia Commons.
Step 4: Clean the data problems the formula can't see
Many "wrong formula" problems are really data problems. Before blaming the formula, check for these:
- Numbers stored as text. They're often left-aligned and get skipped by
SUMandSUMIFS.=ISNUMBER(C2)tells you quickly. Fix the source data or convert withVALUE. - Dates stored as text. Date comparisons silently fail on text.
=ISNUMBER(A2)works here too, since real dates are numbers underneath. - Stray spaces. "West " with a trailing space won't match "West".
TRIMfixes it, and a helper column or a one-time cleanup is usually better than trimming inside every formula. - Duplicate keys in lookup tables. Lookups return the first match. Use
COUNTIFon the key column to find duplicates before you rely on a lookup.
You can ask the model to write these checks for you: "Give me formulas that flag rows where Amount is text, Date is text, or Region has leading or trailing spaces." Those checks are simple and easy to verify.
Step 5: Make it survive the sheet growing
A formula that works today can break next month when rows are added. A few habits help:
- Use tables or open ranges. In Excel, convert the data to a Table and use structured references like
Orders[Amount], which expand automatically. In Google Sheets, open-ended ranges likeA2:Agrow with the data. - Avoid hardcoded values. If the formula contains
2026or a tax rate, move it into a labeled input cell and reference that cell. - Watch out for volatile functions.
INDIRECT,OFFSET,NOW, andTODAYrecalculate constantly and can slow large sheets. Ask the model for a non-volatile alternative if it uses them heavily. - Leave a note. Add a cell comment with a plain-language description of what the formula does. Future you, or a coworker, will thank you.
Step 6: Know when a formula is the wrong tool
If the model's answer is a ten-level nested formula, that's a sign the job may belong elsewhere. Consider asking for one of these instead:
- A pivot table for summaries by category and period. It's faster to build and easier to audit than a grid of
SUMIFS. - Power Query in Excel or a cleanup step for reshaping and cleaning imported data before formulas touch it.
- A short script (Apps Script for Google Sheets, or Office Scripts or VBA for Excel) when you need loops, external data, or logic that's painful in a single cell. Review any generated script carefully before running it, especially if it writes or deletes data, and test it on a copy of the file.
A quick checklist
- Did I name the app, version, and locale?
- Did I give the real layout and a few real sample rows, including messy ones?
- Does every lookup use exact match, and is "not found" handled on purpose?
- Did I test it against known answers, including blanks, text numbers, and date boundaries?
- Will it still work when rows are added?
- Can someone else read it and tell what it does?
The bottom line
LLMs are good at remembering syntax you've forgotten and at suggesting functions you didn't know existed. They're not good at knowing your sheet. Give the model your app, your layout, and real sample data, ask it to explain its answer, and test the result against numbers you can check yourself. That turns a quick AI suggestion into a formula you can actually put in front of your boss or your accountant.
Comments
Post a Comment