Skip to content

SCAN

Walks through an array with a LAMBDA, carrying a running result, and returns the result after every step, such as a running total.

Syntax

=SCAN(, , )

LAMBDA and LET

Excel for Microsoft 365, Excel 2024 and later, and Excel for the web. Not available in Excel 2021 or earlier.

Try it

Edit the formula in G2 or change the inputs. The sheet recalculates as you type.

G2
A
B
C
D
E
F
G
1
Month
Net cash flow
Opening balance
1000
2
Jan
500
500
3
Feb
-200
300
4
Mar
350
650
5
Apr
-450
200
6
May
100
300
7
Jun
250
550
8

G2 =SCAN(0,B2:B7,LAMBDA(acc,v,acc+v)) returns 500; 300; 650; 200; 300; 550

How SCAN works

SCAN is REDUCE with the working shown. It makes the same walk through an array, combining the running result with each value, but instead of returning only the final answer, it returns the result after every step, in an array the same size as the input.

The arguments

  • initial_value is where the running result starts: 0 for a total, or a real opening balance from a cell.
  • array is the data to walk through.
  • lambda has two parameters, the accumulator and the current value, and returns the new accumulator.

=SCAN(0,B2:B7,LAMBDA(acc,v,acc+v)) returns a running total: each cell of the result is the total of that month and all the months before it.

Running totals in one formula

The usual way to build a running total is to copy a formula such as =SUM($B$2:B2) down a column, with careful dollar signs. SCAN does it in one formula in one cell. The result spills, grows with the data, and can't be broken by a stray overwrite.

Balances

Give SCAN an opening balance from a cell and it becomes a ledger. =SCAN(E1,B2:B7,LAMBDA(bal,x,bal+x)) starts at the opening balance in E1 and applies each month's net cash flow to it.

Other running calculations

The LAMBDA can do anything with the accumulator:

  • Running count. =SCAN(0,B2:B7,LAMBDA(n,v,n+(v<0))) counts the negative months so far.
  • Running maximum. =SCAN(0,B2:B7,LAMBDA(m,v,MAX(m,v))) returns the highest value seen so far.
  • Compounding. =SCAN(1000,SEQUENCE(4),LAMBDA(a,i,a*1.5)) grows 1,000 by 50% four times. The values 1 to 4 only set the number of steps; the LAMBDA ignores them.

SCAN and REDUCE

REDUCE returns only the last value, and SCAN returns them all. If you ever write a REDUCE and want to see what it is doing, change it to SCAN and look at the intermediate numbers. The final cell of the SCAN result is what REDUCE would have returned.

Rules

The accumulator must be a single value at each step. If the LAMBDA returns an array, SCAN returns #CALC!. The result is a spilled array, so #SPILL! means the cells it needs aren't empty. A range is read across each row and then down, and the result has the same shape as the range.

See LAMBDA for how to write the function, and MAP when each item can be calculated on its own, without a running result.

SCAN needs Microsoft 365, Excel 2024 or later, or Excel for the web.

Arguments

initial_valueoptional
The starting value of the accumulator, such as 0 for a running total or an opening balance. It is best to supply it, so the first step is clear.
array
The range or array to walk through. The result has the same size and shape.
lambda
A LAMBDA with two parameters: the first receives the accumulator, the second the current value. What it returns is stored in the result and carried to the next step.

Examples

Each example is a live sheet. Change the inputs or the formula and watch the result.

Example 1

A running total

G2 =SCAN(0,B2:B7,LAMBDA(acc,v,acc+v))

Result: 500; 300; 650; 200; 300; 550

Each cell holds the total of that month and every earlier one: 500, 500-200, 300+350 and so on.

G2
A
B
C
D
E
F
G
1
Month
Net cash flow
Opening balance
1000
2
Jan
500
500
3
Feb
-200
300
4
Mar
350
650
5
Apr
-450
200
6
May
100
300
7
Jun
250
550
8

G2 =SCAN(0,B2:B7,LAMBDA(acc,v,acc+v)) returns 500; 300; 650; 200; 300; 550

Example 2

A balance that starts from an opening figure

G2 =SCAN(E1,B2:B7,LAMBDA(bal,x,bal+x))

Result: 1500; 1300; 1650; 1200; 1300; 1550

The opening balance in E1 is 1,000. After January it is 1,500, and the closing balance in June is 1,550.

G2
A
B
C
D
E
F
G
1
Month
Net cash flow
Opening balance
1000
2
Jan
500
1500
3
Feb
-200
1300
4
Mar
350
1650
5
Apr
-450
1200
6
May
100
1300
7
Jun
250
1550
8

G2 =SCAN(E1,B2:B7,LAMBDA(bal,x,bal+x)) returns 1500; 1300; 1650; 1200; 1300; 1550

Example 3

Count the negative months so far

G2 =SCAN(0,B2:B7,LAMBDA(n,v,n+(v<0)))

Result: 0; 1; 1; 2; 2; 2

The count goes up by one in February and April, the two months with a negative cash flow.

G2
A
B
C
D
E
F
G
1
Month
Net cash flow
Opening balance
1000
2
Jan
500
0
3
Feb
-200
1
4
Mar
350
1
5
Apr
-450
2
6
May
100
2
7
Jun
250
2
8

G2 =SCAN(0,B2:B7,LAMBDA(n,v,n+(v<0))) returns 0; 1; 1; 2; 2; 2

Example 4

Compound growth of 50% a step

A1 =SCAN(1000,SEQUENCE(4),LAMBDA(a,i,a*1.5))

Result: 1500; 2250; 3375; 5062.5

SEQUENCE(4) only gives SCAN four steps. The LAMBDA ignores the step number i and multiplies the running value by 1.5 each time.

A1
A
B
C
D
E
F
G
1
1500
2
2250
3
3375
4
5062.5
5
6
7
8

A1 =SCAN(1000,SEQUENCE(4),LAMBDA(a,i,a*1.5)) returns 1500; 2250; 3375; 5062.5

Your turn.

Solve it in the sheet. Every answer is checked, with hints if you need them.

Task 1 of 2

In G2, return the cumulative units sold at the end of each week, as a running total of B2:B6.

Answer in G2.

G2
A
B
C
D
E
F
G
H
I
1
Week
Units sold
Opening stock
500
2
Wk 1
40
3
Wk 2
55
4
Wk 3
30
5
Wk 4
65
6
Wk 5
50
7
8
9
10

Common mistakes

  • Remember the opening balance. SCAN(0,...) starts at zero, which gives the changes but not the balance. Put the opening figure in as the initial value.
  • The result is a spilled array with one cell per input value. #SPILL! means something sits in the cells below the formula.
  • The LAMBDA must return one value at each step. An array result returns #CALC!.
  • The first parameter is the accumulator and the second is the current value. Swap them and the formula still runs but returns wrong numbers.
  • If the input contains an error, every later value that builds on it is an error too, as with any running total.

Questions people ask

How do I make a running total with one formula?

Use `=SCAN(0,B2:B7,LAMBDA(acc,v,acc+v))`. It returns the cumulative total for every row and grows when the data does. No fill-down is needed.

What is the difference between SCAN and REDUCE?

They take the same arguments. SCAN returns the accumulator after every step, as an array. REDUCE returns only the final value.

How do I start a running balance from an opening balance?

Use the cell with the opening balance as the first argument: `=SCAN(E1,B2:B7,LAMBDA(bal,x,bal+x))`.

Is SCAN better than SUM with a fixed first cell?

For a simple running total, either works. SCAN is one formula in one cell, so there is nothing to fill down or accidentally overwrite, and it handles running calculations that SUM cannot, such as running maximums and compound growth.

Does SCAN work in Excel 2021?

No. It is available in Microsoft 365, Excel 2024 and later, and Excel for the web. Earlier versions return `#NAME?`.

Learn SCAN in a real sheet.

Tracks take you from the basics to modelling, one graded exercise at a time.