Dynamic arrays function
SCAN function in Google Sheets and Excel
SCAN walks through a range carrying a running value and spills every step, which makes running totals one formula.
Checked against Microsoft's and Google's documentation on · Example calculated on a live sheet
- Excel 2016
- Excel 2019
- Excel 2021
- Excel 2024
- Microsoft 365
- Google Sheets
Syntax
SCAN([initial_value], array, lambda(accumulator, value, body))Arguments in [brackets] are optional. Google Sheets writes it SCAN(initial_value, array_or_range, LAMBDA).
Example
=SCAN(0,B2:B7,LAMBDA(t,v,t+v)) reads: Start at 0, walk down 2025's monthly revenue in B2:B7, and at each month add the value v to the running total t, spilling every step.
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Month | 2025 | 2026 | 2025 to date |
| 2 | Jan | $18,400 | $20,100 | $18,400 |
| 3 | Feb | $17,200 | $19,850 | $35,600 |
| 4 | Mar | $21,000 | $23,400 | $56,600 |
| 5 | Apr | $19,800 | $22,900 | $76,400 |
| 6 | May | $22,500 | $21,700 | $98,900 |
| 7 | Jun | $24,100 | $26,800 | $123,000 |
D2 shows $18,400 (January 2025) and D7 $123,000, the half-year total.
How the spreadsheet calculates it
Like REDUCE, but returns every step (spills). Here it's given 0, B2:B7 and the result of LAMBDA (step 1).
- 1. LAMBDA: Describes your own function: given
TandV, it works outTplusV.parameter=`T`: a name for each input and `p`calculation=`V`: what to work out with themcalculation=`T+V`: what to work out with them
- 2. SCAN: Like REDUCE, but returns every step (spills). Here it's given
0,B2:B7and the result of step 1.start=`0`: the value before the first cellrange=`B2:B7`: the cells to walk throughLAMBDA(total, value, calculation)=the result of step 1: how to combine the running total with each cell
More SCAN examples
=SCAN(0,C2:C7,LAMBDA(t,v,t+v))The running total for 2026.=SCAN(0,B2:B7,LAMBDA(m,v,MAX(m,v)))The best month so far at each row.=SCAN(1000,B2:B7,LAMBDA(bal,v,bal-v))A balance that starts at 1,000 and goes down by each value.=SCAN("",A2:A7,LAMBDA(s,v,s&LEFT(v,1)))The first letters of the months joined up step by step.
Paste any of these into the formula explainer to see it step by step.
Tips
- Use 0 as the start for sums and counts, and 1 for products.
- Put the formula on the same row as the first value so each step sits beside the month it belongs to.
- For a running balance, start from the opening balance instead of 0.
- Read the LAMBDA aloud as 'total so far, next value, new total' to check it.
What SCAN is used for
Running totals for revenue or spending, cumulative counts, a running balance from deposits and withdrawals, and the best value so far in a series of results.
Arguments
| Argument | Required | What it is |
|---|---|---|
| initial_value | No | The starting value of the running result: 0 for a running total, 1 for a running product. Shown as optional in Microsoft's syntax; give it anyway for clarity. |
| array | Yes | The cells to walk through, in order. |
| lambda | Yes | A LAMBDA with two parameters: the running value so far and the current cell, such as LAMBDA(t, v, t+v).Watch out: Swapping the parameters' meaning (value first) gives a different, wrong running result. |
Common mistakes
Starting from the first cell instead of 0: it gets counted twice.
Writing the LAMBDA with one parameter: SCAN always passes two (the running value and the current cell), so it gives #VALUE!.
Pointing it at a range that isn't in the order you want to accumulate in; SCAN follows the range's order.
Confusing it with REDUCE, which returns only the final value instead of every step.
In Google Sheets
Google Sheets has SCAN, written SCAN(initial_value, array_or_range, LAMBDA). Google's syntax line shows initial_value without brackets, so always give it; the example's form works in both apps.
Errors you may see with SCAN
Questions
How do I make a running total with one formula?
SCAN(0, B2:B7, LAMBDA(t, v, t+v)), as in the example. The classic alternative is SUM($B$2:B2) filled down, which works in every version.
What's the difference between SCAN and REDUCE?
Both carry a value through the range. SCAN spills every intermediate step (a running total); REDUCE returns only the last one (the grand total).
Can SCAN do a running maximum?
Yes: SCAN(0, B2:B7, LAMBDA(m, v, MAX(m, v))) shows the highest month so far at each row. Any rule that combines the result so far with the next value works.
Why is my running total off by one row?
Usually the range starts on the wrong row (a header, or a row late). Make the range start on the first value and the spill will line up beside it.
Sources
Official documentation the facts on this page were checked against.