Sub topic 12
Applying One Formula to a Whole Range with ARRAYFORMULA
This chapter assumes you can write a formula that starts with =, read A1 references, tell relative references from absolute ones, and use ranges such as B2:B10. It also assumes you have seen open-ended ranges such as B2:B. Here you will learn how to make one formula fill a whole column and how to avoid the errors that come with it.
Why one formula per row does not scale
Suppose column B holds quantities, column C holds unit prices, and column D should hold the line total. The usual approach is to type =B2*C2 in D2 and fill it down. That works, but it has three weaknesses:
- When someone adds a row at the bottom, they must remember to copy the formula down.
- Anyone can overwrite one cell in the column, leaving a hardcoded number among formulas.
- The sheet stores hundreds or thousands of separate formulas that must all stay consistent.
ARRAYFORMULA addresses these problems. It lets a single formula in one cell work on a whole range and write its results into the cells below (or beside) it.
How ARRAYFORMULA works
Most formulas expect one value for each argument. If you give them a range, they either reject it or reduce it to a single result, as SUM does. ARRAYFORMULA changes this. It tells Sheets to evaluate the expression once for each position in the ranges and to return a grid of results.
The result is called a spilled array. The formula lives only in the top-left cell, which is the anchor. The cells it fills are output, not separate formulas. If you click one of those output cells, the formula bar is empty or shows the anchor's formula greyed out, depending on the interface.
Here is the basic form:
=ARRAYFORMULA(B2:B*C2:C)This multiplies each cell of B2:B by the cell in the same row of C2:C. Because both ranges are open-ended, the result extends as far as the data does.
Elementwise operations
When two ranges have the same shape, the operation pairs their cells by position: the first with the first, the second with the second, and so on. This is called elementwise evaluation.
| Expression in an ARRAYFORMULA | What it produces |
|---|---|
| `B2:B*C2:C` | Row-by-row product of two columns |
| `B2:B*$F$1` | Each value in B multiplied by one fixed cell |
| `B2:B&" units"` | Each value in B with text appended |
| `A2:A&" "&B2:B` | Two columns joined row by row |
| `B2:B>100` | TRUE or FALSE for each row |
The second row shows that a single cell, such as the tax rate in $F$1, combines with a whole range. Sheets applies it to every element. Absolute references still matter here, because they keep the formula pointing at the right cell if you later copy the whole formula elsewhere.
The empty-row problem
An open-ended range includes every row to the bottom of the sheet, including empty rows. Multiplying two empty cells gives 0, so a naive formula fills the column with zeros far below your data:
=ARRAYFORMULA(B2:B*C2:C)To avoid this, wrap the calculation in an IF that checks for blanks. This is the standard pattern:
=ARRAYFORMULA(IF(B2:B="","",B2:B*C2:C))Read it as: for each row, if column B is empty, return an empty string; otherwise return the product. Inside ARRAYFORMULA, IF also works elementwise, so the test is applied to every row separately.
Two details are worth knowing:
- The empty string
""is not truly empty. The cell looks blank, but functions such asCOUNTAcount it. If other formulas rely on blank cells, check how they treat"". - Test the column that signals a real record. If column B can be blank while column C holds data, test the right column or test both with
OR.
A header row inside the formula
You can place the formula in the header cell and let it write its own header. Use { } to build a small array. A comma places items side by side, and a semicolon stacks them vertically:
=ARRAYFORMULA({"Line total"; IF(B2:B="","",B2:B*C2:C)})Place this in D1. The first element is the header text, and the second is the column of results, which spills below it. This keeps the whole column controlled by one formula, including its title. The pieces you stack must have the same number of columns, or Sheets returns an error.
Functions that need ARRAYFORMULA and functions that do not
Some functions already accept ranges and return arrays, so they spill without help. Others take a single value, so they need ARRAYFORMULA to run over a range.
| Function | Behavior with a range |
|---|---|
| `SUM`, `AVERAGE`, `COUNT` | Reduce the range to one value; `ARRAYFORMULA` does not change that |
| `SORT`, `FILTER`, `UNIQUE` | Return an array on their own |
| `IF`, `AND`, `OR` | Need `ARRAYFORMULA` to work row by row |
| `VLOOKUP` | Needs `ARRAYFORMULA`, and the search key must be a range |
| `LEFT`, `RIGHT`, `LEN`, `TEXT` | Need `ARRAYFORMULA` to run on each cell |
The AND and OR entries trip people up. Even inside ARRAYFORMULA, they collapse a whole range into one TRUE or FALSE. For row-by-row logic, use multiplication for AND and addition for OR:
=ARRAYFORMULA(IF((B2:B>100)*(C2:C<5),"Review",""))The product of two TRUE/FALSE tests is 1 only when both are true, so it acts as AND.
Spill errors and blocked output
An array formula needs empty space for its output. If any cell in the output area already has content, the formula returns #REF! and a message such as "Array result was not expanded because it would overwrite data in D5".
To fix it:
- Click the cell with the error and read the message to find the blocking cell.
- Delete the contents of that cell, or move the formula to a place with room.
- Check for stray spaces, since a cell holding a single space counts as occupied.
This also explains an important rule: do not fill the cells under an array formula with your own data, and do not also fill the formula down by hand.
Combining ARRAYFORMULA with other functions
Here is a worked example. Column A holds order IDs, column B holds quantities, and column C holds prices. Cell F1 holds a tax rate, such as 0.2. You want a total with tax per row, using one formula in D2:
=ARRAYFORMULA(IF(A2:A="","",B2:B*C2:C*(1+$F$1)))Each part has a role:
A2:A=""detects unused rows, so the result stays blank below the data.B2:B*C2:Ccalculates the base amount per row.(1+$F$1)applies the tax rate. The$signs keep the reference fixed.
A common companion is a lookup from another sheet:
=ARRAYFORMULA(IF(A2:A="","",VLOOKUP(A2:A,Products!A:C,3,FALSE)))For each ID in column A, this finds the matching row in the Products sheet and returns the value in its third column. The first argument is a range, which is what makes it run for every row.
Totals that read an array formula's output
You can sum a column that an array formula fills. Because the empty-row pattern returns "", SUM ignores those text cells and gives the correct total:
=SUM(D2:D)Keep this formula outside column D, or it will refer to itself. A reference to a range that contains the formula's own cell creates a circular dependency, and Sheets reports it as an error.
Performance and design trade-offs
An open-ended ARRAYFORMULA is convenient, but it has costs:
- Each formula evaluates the full range down to the last row of the sheet. With many such formulas and a large sheet, recalculation slows down.
- You can limit the range, for example
B2:B1000, if you know the data stays within that size. You then give up automatic growth. - A single formula in one cell is easy to break by accident. Deleting or editing the anchor removes the entire column of results. Consider protecting the cell.
- Sorting or filtering the sheet moves ordinary rows but not the single anchor in a predictable way, so keep array-formula columns outside data you sort, or sort with a formula such as
SORT.
A decision guide:
| Situation | Better choice |
|---|---|
| Data grows often and rows are added at the bottom | `ARRAYFORMULA` with open-ended ranges |
| Data is fixed and small | Fill the formula down |
| Users need to override individual rows | Fill down, since array output cannot be edited cell by cell |
| Data is imported or generated by another formula | `ARRAYFORMULA` or a spilling function |
Common mistakes
- Forgetting the empty-row test and getting zeros or
#DIV/0!in unused rows. - Mixing ranges of different sizes, such as
B2:B10*C2:C5, which gives an error because the shapes do not match. - Placing the formula in a cell whose output area contains data.
- Using
ANDorORfor row-by-row logic. - Writing the formula in row 2 but including row 1 in the range, so the header text gets multiplied and produces
#VALUE!.
The flow below summarizes how Sheets evaluates the formula.
Exercise
Column A holds product names, column B holds quantities, and column C holds prices, with data starting in row 2. Write one formula for D2 that shows the quantity times the price for every row, stays blank for rows where column A is empty, and keeps working when new rows are added.
Show answer
=ARRAYFORMULA(IF(A2:A="","",B2:B*C2:C))
Exercise
Your array formula in E2 shows #REF! with the message that the result would overwrite data in E7. What are two ways to fix it, and which rule does this error come from?
Show answer
Delete the contents of E7 (including a stray space), or move the formula to a place where the output area is empty. The error comes from the rule that an array formula needs empty cells for its whole output, so any existing content in that area blocks it.
Exercise
Why does =ARRAYFORMULA(IF(AND(B2:B>100,C2:C<5),"Review","")) not give a row-by-row result, and what is the fix?
Show answer
AND collapses the whole range into a single TRUE or FALSE, so every row gets the same answer. Multiply the tests instead: =ARRAYFORMULA(IF((B2:B>100)*(C2:C<5),"Review","")).