Logical function

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))
ArgumentWhat it means
initial_valueOptionalThe starting value of the accumulator, such as 0 or an opening stock level.
arrayRequiredThe values to process in order.
lambdaRequiredA 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

C2fx=SCAN(0, B2:B6, LAMBDA(acc, x, acc+x))
ABC
1DaySales
2Mon120120
3Tue95215
4Wed140355
5Thu80435
6Fri110545

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

C2fx=SCAN(50, B2:B5, LAMBDA(stock, m, stock+m))
ABC
1MovementUnits
2Delivery2070
3Sale-1555
4Sale-3025
5Delivery4065

The warehouse starts with 50 units. After each movement the balance is 70, 55, 25 and 65.

3Best month so far

C2fx=SCAN(0, B2:B6, LAMBDA(best, x, MAX(best, x)))
ABC
1MonthSales
2Jan300300
3Feb450450
4Mar420450
5Apr510510
6May480510

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.

⬇Practice workbookExamples with live formulas + practice sheets · .xlsx · 16 KB

Common errors and how to fix them

ErrorWhy it happensFix
#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.

Recipes that use SCAN