SCAN function
Runs a LAMBDA through an array with an accumulator and returns every intermediate result, e.g. a running total or running balance.
Available in Microsoft 365, Excel 2024, Excel for the web, Excel for Mac 2024
Excel 2024 and laterWorks in Google Sheets
Syntax
SCAN([initial_value], array, lambda(accumulator, value))| Argument | What it means | |
|---|---|---|
initial_value | Optional | The starting value of the accumulator, such as 0 or an opening stock level. |
array | Required | The values to process in order. |
lambda | Required | A LAMBDA with two parameters, the accumulator and the current value. Its result is both shown and carried to the next step. |
Returns: An array the same size as array containing the accumulator after each step; it spills.
How SCAN works
SCAN is the step-by-step version of REDUCE. It moves through a list, combines each item with the running value using your LAMBDA, and writes down the running value after every step. The classic use is a running total: day 1, day 1 + day 2, day 1 + day 2 + day 3, and so on — as a single spilled formula instead of a column of =C2+B3 formulas copied down.
Because the rule is a LAMBDA, the "running" calculation does not have to be addition. You can track a stock balance from an opening level, a running maximum (the best result so far), or a balance that should never drop below zero. The output has the same size as the input, so it lines up next to the source data.
Examples
1Running total of daily sales
| A | B | C | |
|---|---|---|---|
| 1 | Day | Sales | |
| 2 | Mon | 120 | 120 |
| 3 | Tue | 95 | 215 |
| 4 | Wed | 140 | 355 |
| 5 | Thu | 80 | 435 |
| 6 | Fri | 110 | 545 |
Starting at 0: 120, then 120 + 95 = 215, then 355, 435 and finally 545 for the week. One formula fills C2:C6.
2Stock balance from an opening level
| A | B | C | |
|---|---|---|---|
| 1 | Movement | Units | |
| 2 | Delivery | 20 | 70 |
| 3 | Sale | -15 | 55 |
| 4 | Sale | -30 | 25 |
| 5 | Delivery | 40 | 65 |
The warehouse starts with 50 units. After each movement the balance is 70, 55, 25 and 65.
3Best month so far
| A | B | C | |
|---|---|---|---|
| 1 | Month | Sales | |
| 2 | Jan | 300 | 300 |
| 3 | Feb | 450 | 450 |
| 4 | Mar | 420 | 450 |
| 5 | Apr | 510 | 510 |
| 6 | May | 480 | 510 |
Each step keeps the larger of the record so far and the current month, giving a running maximum: the record only changes in February and April.
Common errors and how to fix them
| Error | Why it happens | Fix |
|---|---|---|
#VALUE! | The LAMBDA does not have two parameters. | Use LAMBDA(acc, x, ...). |
#SPILL! | The cells where the results should appear are not empty. | Clear the output range. |
Tips
- Text works too:
=SCAN("", A2:A5, LAMBDA(a, x, a&x))builds up a string step by step. - To restart a running total for each new group (e.g. each month), combine SCAN with an IF that resets the accumulator when the group label changes, or use SUMIFS with an expanding range instead.
Google Sheets
SCAN also exists in Google Sheets and works the same way for everyday use, so the examples above work there too.