Sub topic 12
Applying date formulas to whole columns
This chapter assumes you can enter formulas, use relative and absolute references, and apply number formats from the Format menu. It also assumes you know DATE, EOMONTH, EDATE, TODAY and date subtraction from earlier chapters. Here you apply those functions to a whole column from a single formula instead of copying a formula down row by row.
Why use one formula for a whole column
The usual approach is to write a formula in row 2 and drag the fill handle down. It works, but it has three costs:
- A new row at the bottom has no formula until you copy one in.
- Someone can overwrite one cell in the middle, and the column silently becomes inconsistent.
- Every row holds its own copy of the formula, which is harder to audit.
ARRAYFORMULA fixes this by running one formula over a range and returning one result per row. The results appear in the formula cell and in the cells below it. You maintain one formula, and new rows are covered as long as your range includes them.
ARRAYFORMULA basics
A formula normally works on single values. If you pass a range where a single value is expected, Sheets either uses only the first cell or returns an error. Wrapping the formula in ARRAYFORMULA tells Sheets to evaluate it for every element of the range.
Suppose column A holds dates starting in A2, with a header in A1. This formula goes in B2:
=ARRAYFORMULA(EOMONTH(A2:A, 0))It returns the last day of each date's month, down the column. Sheets fills B2, B3, B4 and so on automatically. You can only type in B2. The cells below it show the results but belong to the formula.
The range A2:A is open-ended. It means "from A2 to the bottom of the column", so rows you add later are included.
Blank rows become 1899
An open-ended range includes every empty cell below your data. Sheets treats an empty cell as 0 in date arithmetic, and date serial number 0 is 1899-12-30. Without a guard, the formula above fills hundreds of rows with 1899-12-31 (the end of that month).
The standard fix is to test for blanks and return an empty string:
=ARRAYFORMULA(IF(A2:A="", "", EOMONTH(A2:A, 0)))IF also needs the ARRAYFORMULA wrapper here. Inside it, the condition A2:A="" becomes a column of TRUE/FALSE values, and the formula picks a result row by row.
Computing across two columns
You can combine columns in the same formula. With start dates in A and end dates in B, this returns the days between them:
=ARRAYFORMULA(IF((A2:A="")+(B2:B="")>0, "", B2:B - A2:A))The expression (A2:A="")+(B2:B="") adds two columns of TRUE/FALSE values. TRUE counts as 1, so the sum is greater than 0 when either cell is blank.
Do not write OR(A2:A="", B2:B="") here. OR and AND collapse a whole range into one TRUE or FALSE value, so you get a single answer for the entire column, not one per row. Use addition for "or", and multiplication for "and".
For elapsed hours, multiply by 24 as you did for a single row:
=ARRAYFORMULA(IF((A2:A="")+(B2:B="")>0, "", (B2:B - A2:A) * 24))Adding a header from the same formula
You can output the column header and the results together by building an array with curly braces. The semicolon stacks values vertically:
={"Month end"; ARRAYFORMULA(IF(A2:A="", "", EOMONTH(A2:A, 0)))}Put this in B1. The header is the first element of the array, and the results follow beneath it. This keeps the title and the formula in one cell, so nobody overwrites one without the other.
Which functions work inside ARRAYFORMULA
Most date functions work row by row once wrapped in ARRAYFORMULA. The table lists the ones from this tutorial.
| Formula pattern | Result per row |
|---|---|
| `YEAR(A2:A)`, `MONTH(A2:A)`, `DAY(A2:A)` | The date's year, month or day number |
| `WEEKDAY(A2:A, 2)` | 1 (Monday) to 7 (Sunday) |
| `EDATE(A2:A, 3)` | The date three months later |
| `EOMONTH(A2:A, 0)` | The last day of the date's month |
| `DATEVALUE(A2:A)` | A real date from a text date |
| `TEXT(A2:A, "yyyy-mm-dd")` | The date as formatted text |
| `B2:B - A2:A` | Days between two columns |
Some functions are less predictable with ranges, especially ones that take several range arguments, such as NETWORKDAYS with a holiday list. If ARRAYFORMULA returns only one value or an error, use MAP with LAMBDA, which calls a function once per row:
=MAP(A2:A, B2:B, LAMBDA(s, e,
IF(OR(s="", e=""), "", NETWORKDAYS(s, e, Holidays!A2:A20))))Inside LAMBDA, s and e are single values for one row, so OR works as you would expect there. MAP is slower than a native array operation on very large ranges, so use it only when ARRAYFORMULA does not work.
Converting a column of text dates
A common use is cleaning an imported column of text dates. This converts every row and leaves blanks empty:
=ARRAYFORMULA(IF(A2:A="", "", DATEVALUE(A2:A)))The output is a column of serial numbers, so format it as a date. If some cells in the column are already real dates, DATEVALUE may return an error for those rows. You can fall back to the original value:
=ARRAYFORMULA(IF(A2:A="", "", IFERROR(DATEVALUE(A2:A), A2:A)))IFERROR also hides genuine bad input, such as a cell containing "n/a". Use it when you know what the failures look like, and check the result with a count of non-date cells.
Keeping the formula working
Array formulas fail in a few predictable ways.
The spill is blocked
Sheets needs every cell below the formula to be empty. If any of them holds a value, the formula shows #REF! with the message "Array result was not expanded because it would overwrite data". Clear the blocking cell, or move the formula to a column with nothing below it. A manually typed value left from an earlier fill-down is the usual cause.
Mixed formulas in one column
Do not put an array formula in B2 and also leave drag-down formulas in B3:B100. The drag-down cells block the spill. Delete the old formulas first.
Whole-column references are heavy
A2:A is convenient, but Sheets evaluates it to the end of the sheet's rows. On a sheet with thousands of empty rows, many array formulas slow recalculation. If your data has a known ceiling, use a bounded range such as A2:A1000. If you need open-ended behavior, delete unused rows at the bottom of the sheet so the range stays short.
Sorting and filtering
Sorting a range that contains an array formula's output can cause problems, because the output cells are not independent values. Sort the source data, and let the formula recalculate. The same applies to filters: the output cells follow the source rows.
Volatile inputs
If the formula includes TODAY() or NOW(), the whole column recalculates when the sheet does. The result is correct, but every row changes at once. This array returns the days remaining until each due date in column A:
=ARRAYFORMULA(IF(A2:A="", "", A2:A - TODAY()))The value in each row decreases by 1 each day. If you need a fixed snapshot, paste the values instead of keeping the formula.
Choosing between fill-down and ARRAYFORMULA
| Situation | Better choice |
|---|---|
| Data grows by adding rows at the bottom | `ARRAYFORMULA` |
| Users sometimes edit single cells in the result column | Fill-down formulas |
| The formula needs a function that rejects ranges | `MAP` with `LAMBDA`, or fill-down |
| The sheet has tens of thousands of rows | Bounded `ARRAYFORMULA` range |
| You want a fixed record that never changes | Paste values only |
Exercise
Column A holds order dates from A2 downward and column B holds delivery dates. Write one formula for C2 that returns the number of days between them for each row, leaves the row empty if either date is missing, and keeps working as new rows are added.
Show answer
=ARRAYFORMULA(IF((A2:A="")+(B2:B="")>0, "", B2:B - A2:A)). The sum of the two blank tests is greater than 0 when either cell is empty. OR is not used because it would reduce the whole column to one TRUE or FALSE value.
Exercise
You put =ARRAYFORMULA(EOMONTH(A2:A, 0)) in B2 and every empty row below your data shows 1899-12-31. Explain why, and give the corrected formula.
Show answer
Empty cells count as 0, which is the serial number for 1899-12-30, so EOMONTH returns the end of that month. Test for blanks first: =ARRAYFORMULA(IF(A2:A="", "", EOMONTH(A2:A, 0))).
Exercise
Your array formula in D2 shows #REF! with the message that the array result was not expanded. What is the most likely cause, and how do you fix it?
Show answer
A cell below D2 in the spill area already contains a value, often an old drag-down formula or typed value. Clear those cells, or move the array formula to a column where the cells beneath it are empty.