Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

How to Combine SCAN and Regex in Excel for Running Counts

Build a running count with SCAN and REGEXTEST, correct common pattern errors, and choose a simpler approach for independent text extraction.

Table of Contents

Combine SCAN with REGEXTEST when each row's result must depend on the preceding rows—for example, a running count of descriptions containing an order number. Use a per-cell formula or MAP when you only need independent text extraction.

These examples require an Excel version that supports the functions used. SCAN is available in Microsoft 365 and Excel 2024; the regex functions are documented for Microsoft 365. If Excel returns #NAME?, check function availability in your installation.

How SCAN carries a result forward

=SCAN(initial_value, array, LAMBDA(accumulator, value, calculation))
  • initial_value: the starting result; use 0 for a counter.
  • array: the cells to process.
  • accumulator: the result from the previous step.
  • value: the current cell.

SCAN returns every intermediate result, so a vertical input range produces a column of running results. Set the initial value explicitly to make the calculation clear.

Extract dollar amounts from descriptions

If G2 contains Received $250 Payment, this formula returns $250:

=IFERROR(REGEXEXTRACT(G2,"[$][0-9]+([.][0-9]{1,2})?"),"")

The pattern matches a dollar sign, digits and an optional decimal portion. It is intended for simple amounts such as $250 or $75.50; it does not validate every currency format or handle thousands separators.

Fill the formula down for independent rows. Alternatively, where MAP is available, process a bounded range in one spilled formula:

=MAP(G2:G11,LAMBDA(item,IFERROR(REGEXEXTRACT(item,"[$][0-9]+([.][0-9]{1,2})?"),"")))

The returned amounts are text. For numeric results from this simple format, remove the dollar sign and convert the remaining string with Excel's VALUE function, accounting for your regional decimal separator.

How to combine SCAN and REGEX functions in Excel to solve complex problems Picture 1

The original workbook illustration is retained for context; use the corrected formulas above.

Count matching order IDs as you move down a column

Suppose E2:E100 contains order descriptions. To count rows containing a sequence of at least four digits, enter this formula in an empty cell outside the input range:

=SCAN(0,E2:E100,LAMBDA(running,item,running+IFERROR(--REGEXTEST(item&"","[0-9]{4,}"),0)))

REGEXTEST returns TRUE for a match. The double minus converts TRUE/FALSE to 1/0, which SCAN adds to the previous count. For inputs AB1234, pending and 5678, the results are 1, 1, 2.

How to combine SCAN and REGEX functions in Excel to solve complex problems Picture 2

Match the rule you actually need

[0-9]{4,} checks for a sequence anywhere in the text; it does not prove the whole value is a valid order ID. If your rule is exactly four digits and nothing else, use ^[0-9]{4}$. Anchors require the whole string to match.

Troubleshooting and choosing the right tool

  • #SPILL!: clear the output area and put the spilling formula outside an Excel table.
  • Unexpected matches: test your pattern on valid, invalid, blank and unusual entries before applying it to the full range.
  • Formula separator errors: some regional settings require semicolons instead of commas between arguments.
  • No running dependency: use a normal filled formula or MAP rather than adding an unused SCAN accumulator.

Review the Excel regex functions before building more complex patterns. For a simple total rather than a running count, a standard counting function may be easier to maintain.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.