# How to use the SCAN function

**What is the SCAN function?**

The SCAN function passes all values in an array to a LAMBDA function, the LAMBDA function calculates new values based on a formula you specify. It then returns an array with the same size as the original array. (This is not always the case.)

#### Table of Contents

## 1. Introduction

**What is an array in Excel?**

An array in Excel is a collection of values arranged in rows and columns. It can be thought of as a table or a grid of data.

There are two types of arrays. One-dimensional arrays: A single row or column of data. Two-dimensional arrays: Data organized in both rows and columns.

There are two types of array formulas: The first type returns a single value and the second type returns multiple values. An array formula is a formula that can perform multiple calculations on one or more sets of values.

Excel 365 subscribers have access to dynamic array formulas, a powerful feature that automatically adjusts its output range. These formulas populate the initial target cell and expand into neighboring cells as needed, adapting their size based on the formula's result. This automatic expansion and contraction of the output range is the key characteristic that gives them the name "dynamic" array formulas. The process of extending results into adjacent cells is known as "spilling.

Excel processes arrays in RAM allowing for rapid computations. However, when array sizes exceed available memory, Windows may resort to using virtual memory on the hard drive or SSD. This fallback to disk storage significantly slows down calculations, as accessing data from these devices is much slower than from RAM.

**What is the LAMBDA function?**

The SCAN function uses the LAMBDA function as its third argument to define how each cell in the array should be calculated. This combination allows you to perform cumulative calculations across a range of values.

The LAMBDA function is required in the SCAN function, you can't leave it out. Read more: LAMBDA function

## 2. Syntax

SCAN([*initial_value*], *array*, *lambda*(*accumulator*, *value, calculation*))

Argument |
Description |

[initial_value] |
Optional. Lets you specify a starting value. |

array |
Required. A cell reference to a cell range or an array. |

lambda(accumulator,value, calculation) |
Required. A lambda function with three arguments: - accumulator - value - calculation |

accumulator |
Required. This is the sum the SCAN function returns. |

value |
Required. Iterates through each value in the array or cell range. |

calculation | Required. A calculation containing the accumulator and value parameters. |

## 3. Example 1

This example demonstrates how to create a reverse running total using the SCAN function. A reverse running total is a cumulative sum calculated from the bottom of a column or range upwards instead of the usual top-to-bottom approach. In other words, it's a running total that starts from the last value in a range and accumulates values moving upwards.

The amazing thing is that the SCAN function begins with the total and then subtracts each number cell by cell downwards. This creates a dynamic Excel 365 array which refreshes automatically when values change.

Formula in cell D5:

This Excel 365 dynamic array formula creates an array that represents a reverse running total, it begins with the total and subtracts each number on the same row until all numbers are processed. Here is how it works:

- SUM(C5:C16): This is the initial value for the SCAN function. It calculates the total sum of values in the range C5:C16.
- C4:C16: This is the range of cells that the SCAN function will process, starting from C4 and going down to C16.
- LAMBDA(a,b,a-N(b)): This is the lambda function that defines how each step of the scan is calculated.

a: represents the accumulator (the running total)

b: represents the current value being processed

N(b): converts b to a number (in case there are any blank cells or text)

a-N(b): subtracts the current value from the accumulator

There is an even smaller reverse running total formula explained here: SUM function It calculates each value cell by cell in contrast to the formula above that returns the entire dynamic array.

## 4. Example 2

**Generate a monthly repayment schedule for a loan with the following details:**

**Loan amount: $12,000****Monthly principal: $1,000****Term: 12 months****Interest rate: 5% per year****Payment frequency: Monthly**

**The table should show how the loan balance decreases over time, breaking down each payment into principal and interest components?**

This example demonstrates a formula that calculates cumulative interest based on a cell range containing loan balance numbers (D9:D20).

The table in the image above shows a linear loan meaning the monthly payment varies contrary to an annuity loan that has a fixed monthly payment.

A linear loan is also known as a straight-line loan or reducing balance loan. Principal repayment (cell D5) is constant throughout the loan term. Interest payment decreases over time as the principal balance reduces (cells E9 and below). Total payment (principal + interest) decreases over the loan term. It has higher initial payments in contrast to an annuity loan.

Formula in cell D3:

This is a simple demonstration of the SCAN function, I know that the formula can be made a lot smaller using only the division, multiplication, and addition operators.

### Explaining the formula

#### Step 1 - Build the LAMBDA function

The LAMBDA function build custom functions without VBA, macros or javascript.

Function syntax: LAMBDA([parameter1, parameter2, …,] calculation)

The LAMBDA function is iterated as many times as there are values in cell range B3:C11.

LAMBDA(a,b,a+b*$D$6/12)

The first argument specifies the accumulator variable, the second argument is the value variable. The third argument is the actual calculation:

a+b*$D$6/12

It calculates the interest for each month and adds it to a total (accumulate variable a).

#### Step 2 - Pass values to the LAMBDA function

The SCAN function lets you pass arrays to the LAMBDA function.

SCAN(0,D9:D20,LAMBDA(a,b,a+b*$D$6/12))

Each value in cell range D9:D20 is put in variable b, it is then added to a total.

### 'SCAN' function examples

The following article has a formula that contains the SCAN function.

What's on this page Reverse text Insert random characters Convert letters to numbers How to shuffle characters in the alphabet […]

### Functions in 'Logical' category

The SCAN function function is one of 16 functions in the 'Logical' category.

## How to comment

How to add a formula to your comment<code>Insert your formula here.</code>

Convert less than and larger than signsUse html character entities instead of less than and larger than signs.

< becomes < and > becomes >

How to add VBA code to your comment[vb 1="vbnet" language=","]

Put your VBA code here.

[/vb]

How to add a picture to your comment:Upload picture to postimage.org or imgur

Paste image link to your comment.

Contact OscarYou can contact me through this contact form