Skip to main content
ExcelSCANLogicalArray FormulasRunning Totals

The Problem

Are you tired of manually dragging formulas down columns to calculate running totals, cumulative sums, or tracking sequential progress in your spreadsheets? Perhaps you've faced the frustration of adding new data rows, only to realize your meticulously crafted formulas don't automatically extend, leaving gaps in your crucial financial reports or project trackers. This constant need to adjust and expand formulas is not only time-consuming but also a breeding ground for errors. Many Excel users find themselves stuck in this repetitive loop, yearning for a more dynamic and automated solution.

What is SCAN? The SCAN function is an Excel function that processes an array by applying a LAMBDA function to each element, accumulating a running total or intermediate value. It is commonly used to calculate cumulative sums, running averages, or other sequential operations across a range, providing a powerful way to handle dynamic, expanding datasets. It offers a modern, elegant alternative to traditional methods, revolutionizing how you approach iterative calculations.

Business Context & Real-World Use Case

In the fast-paced world of business, data accumulates constantly. Imagine you're a finance analyst tracking daily cash flow, a sales manager monitoring cumulative sales against a target, or a project lead needing to see the total project cost incurred over time. Traditionally, achieving these running totals involved placing a formula like =SUM($B$2:B2) in cell C2 and then dragging it down. This method works, but it's brittle. If you insert rows or add new data, you often need to manually adjust the ranges or drag the formula further, which is a significant time sink.

In my years as a data analyst, I've seen teams waste countless hours on exactly this problem. A common mistake we've seen is failing to extend these manual formulas, leading to incorrect reporting, especially when dealing with large datasets or frequently updated transaction logs. For instance, in an inventory management scenario, calculating the running stock level after each transaction is critical. A manual error here could lead to incorrect stock counts, impacting order fulfillment and potentially causing significant financial losses.

Automating these cumulative calculations with Excel's SCAN function provides immense business value. It drastically reduces the potential for human error, ensures data consistency, and frees up valuable time for more strategic analysis rather than formula maintenance. By using SCAN, you can set up a calculation once, and it will automatically expand or contract with your data, providing real-time, accurate running totals without any manual intervention. This agility is indispensable for timely decision-making and robust reporting in any industry.

The Ingredients: Understanding SCAN's Setup

The SCAN function is a dynamic array function that iterates through an array, applying a LAMBDA function to each value, and accumulating a result. It returns an array of intermediate values, showing the state of the accumulator at each step. This makes it incredibly powerful for sequential calculations.

Here's the essential syntax you'll need to master:

=SCAN([initial_value], array, lambda)

Let's break down each parameter with clarity:

Parameter Description
initial_value This is the starting point for your accumulation. Think of it as the 'zero' from which your running total begins. It's the initial value of the accumulator that the lambda function will use in its very first iteration. If omitted, SCAN will typically use the first value of the array as the initial accumulator, but it's best practice to explicitly define it for clarity.
array This is the range or array of values that SCAN will iterate through. For each element in this array, the lambda function will be applied, updating the accumulator. This is your source data for the calculation.
lambda This is the functional heart of SCAN. It's a custom function that defines how the accumulation happens at each step. The lambda must accept two arguments: the current accumulated value (often called accumulator or a) and the current value from the array (often called current_value or v). It then returns the new accumulated value.

Understanding the lambda is crucial. It acts like a mini-program that SCAN executes repeatedly. In its first iteration, the lambda receives the initial_value and the first element of the array. It then calculates and returns a new accumulated value. In subsequent iterations, the lambda receives the previously calculated accumulated value and the next element from the array, continuing this pattern until all elements are processed. This sequential processing is what makes SCAN so effective for running totals.

The Recipe: Step-by-Step Instructions

Let's whip up a practical example: calculating a running total of monthly sales. This is a common requirement for businesses to track performance over time.

First, prepare your ingredients (data) in Excel.

Sample Data: Monthly Sales

Month Sales (Units)
January 150
February 210
March 180
April 250
May 190
June 220

Assume your data is in a spreadsheet as follows:

  • Column A: Month (A2:A7)
  • Column B: Sales (Units) (B2:B7)

We want the running total to appear in Column C.

  1. Select Your Output Cell: Click on cell C2. This is where our SCAN function will begin to display its dynamic array of running totals.

  2. Enter the Formula's Core: Start by typing =SCAN(. We're telling Excel we want to begin a SCAN operation.

  3. Define the Initial Value: For a running total, we need to start from zero. So, our initial_value will be 0. Your formula now looks like: =SCAN(0,

  4. Specify the Array: Next, we point to our sales data. Our array is the range B2:B7. Your formula should now be: =SCAN(0, B2:B7,

  5. Construct the LAMBDA Function: This is where the magic happens. We need a lambda that takes two arguments: the accumulator (let's call it acc) and the current_value from the array (let's call it current_sale). For a running total, we simply add the current_sale to the acc.

    • The lambda syntax is LAMBDA(accumulator, current_value, calculation).
    • So, our lambda will be LAMBDA(acc, current_sale, acc + current_sale).
  6. Complete the Formula: Combine all parts. The full formula you'll enter into cell C2 is:

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

  7. Press Enter: As soon as you press Enter, Excel will spill the results directly into cells C2 through C7, automatically calculating the running total for each month. There's no need to drag!

Expected Output in Column C:

Month Sales (Units) Running Total (C)
January 150 150
February 210 360
March 180 540
April 250 790
May 190 980
June 220 1200

The first value (150) is 0 (initial_value) + 150. The second value (360) is 150 (previous total) + 210. This pattern continues, providing a fully dynamic running total. Should you add more sales data to column B, you can simply adjust the B2:B7 range in your SCAN formula, and it will expand automatically. This is a game-changer compared to traditional methods.

Pro Tips: Level Up Your Skills

Mastering SCAN goes beyond just basic running totals. Here are some expert insights to elevate your usage:

  • Dynamic Data Ranges: Experienced Excel users prefer to make their data ranges dynamic. Instead of hardcoding B2:B7, consider using B2:INDEX(B:B, COUNTA(B:B)) or converting your data into an Excel Table. When your data is in an Excel Table (e.g., named Table1), your array can simply be Table1[Sales (Units)]. This makes the SCAN function truly automatic, expanding or contracting as you add or remove rows from your table. This is perfect for creating Running Totals that automatically expand with your data, eliminating manual formula adjustments.
  • Beyond Sums: While running sums are common, SCAN can perform any cumulative operation. Need a running product? Change acc + current_sale to acc * current_sale. Running average? You'll need to pass more information in your lambda or creatively manage your initial_value to track the count of items processed, perhaps using LET within the LAMBDA.
  • Initial Value Nuances: Don't always default to 0. If you're tracking a running balance that starts with an existing amount, that existing amount should be your initial_value. For example, a bank account balance might start at $1,000, so your initial_value would be 1000.
  • Combining with Other Functions: SCAN shines when combined with other dynamic array functions. You could use FILTER to select specific data before passing it to SCAN, or wrap SCAN within SORT or UNIQUE to process pre-sorted or de-duplicated lists, creating sophisticated, multi-layered calculations with ease.

Troubleshooting: Common Errors & Fixes

Even the most seasoned Excel chefs occasionally encounter a snag. Here’s how to troubleshoot common SCAN issues.

1. #VALUE! Error: Incorrect Lambda Arguments

  • Symptom: You see #VALUE! displayed where your SCAN formula should be.
  • Cause: This is a very common issue when working with SCAN and LAMBDA. The LAMBDA function requires precisely two arguments: one for the accumulated value (the current state of the running total) and one for the current item from the array being processed. If your LAMBDA is defined with one argument (e.g., LAMBDA(x, x)) or three arguments (e.g., LAMBDA(a, b, c, a+b+c)), Excel doesn't know how to pass the two required values, resulting in the #VALUE! error. This happens because SCAN always passes (accumulator, current_item) to its internal lambda.
  • Step-by-Step Fix:
    1. Examine your LAMBDA: Carefully check the LAMBDA portion of your SCAN formula.
    2. Verify argument count: Ensure your LAMBDA has exactly two arguments defined, like LAMBDA(acc, current_value, ...) or LAMBDA(a, v, ...). The names don't matter, but the count does.
    3. Correct the calculation: Make sure the calculation inside the LAMBDA refers to these two arguments correctly. For a running sum, it should typically be acc + current_value.

2. #CALC! Error: Non-Numeric Initial Value or Array Element

  • Symptom: The formula returns #CALC!.
  • Cause: This error often arises when your initial_value or an element within your array is non-numeric, but your LAMBDA attempts to perform a mathematical operation (like addition or multiplication) on it. For example, if your initial_value is "Start" and you try to do "Start" + 5, Excel will error. Similarly, if your sales array contains text like "N/A", the LAMBDA will struggle.
  • Step-by-Step Fix:
    1. Check initial_value: Ensure your initial_value is a number (e.g., 0, 100, etc.) if your LAMBDA performs arithmetic.
    2. Inspect the array: Look for any non-numeric entries within your array range. Text, error values, or blank cells (if not handled) can trigger this.
    3. Clean your data: Use functions like IFERROR, N, or VALUE within your array or LAMBDA to handle non-numeric values gracefully. For instance, LAMBDA(acc, current_value, acc + N(current_value)) will treat text as 0 for addition.

3. #N/A Error: Empty or Invalid Array Reference

  • Symptom: You see #N/A as the result of your SCAN formula.
  • Cause: This usually means that the array argument provided to SCAN is either empty, refers to a non-existent range, or evaluates to an #N/A error itself (perhaps from a preceding XLOOKUP or FILTER function). SCAN needs a valid range of values to iterate through.
  • Step-by-Step Fix:
    1. Verify array range: Double-check that the range specified in your array argument (e.g., B2:B7) actually contains data and refers to a valid part of your sheet.
    2. Check for empty cells: While not always the cause of #N/A, a completely empty array will result in this error.
    3. Trace dependent formulas: If your array is the result of another formula (e.g., FILTER(A:A,...)), check that preceding formula. Ensure it's not producing an #N/A error or an empty array.

Remember, clear communication between your SCAN function and its LAMBDA is key. Always ensure the LAMBDA is structured to correctly receive and process the two values SCAN provides at each iteration.

Quick Reference

For those moments when you need a quick refresh on SCAN:

  • Syntax: =SCAN([initial_value], array, lambda)
  • Purpose: Iterates through an array, applying a LAMBDA function sequentially to accumulate a running total or intermediate result. Returns an array of these intermediate results.
  • Lambda Structure: LAMBDA(accumulator_value, current_array_element, calculation)
  • Most Common Use Case: Creating dynamic running totals or cumulative sums that automatically adjust to expanding datasets, eliminating manual formula dragging.

Related Functions

👨‍💻

Written by The Head Chef

Former 10-year Financial Analyst who survived countless month-end closes. I build these recipes to save you from weekend-ruining spreadsheet errors.

Read the full story →

You might also find these useful 💡