Skip to content

Fix Excel LAMBDA Recursion #NUM Errors for Large Arrays

A user on a popular Excel subreddit recently posted: “I built a recursive LAMBDA to strip special characters from a list. It works perfectly on my test data of 50 rows, but as soon as I apply it to my production sheet with 1,000 rows, I get a #NUM! error. Is there a way to increase the recursion limit in Excel?”

The short answer is no—Excel has a hard limit of approximately 253 to 255 recursive calls to prevent stack overflow. However, you can solve this by switching from recursion to iteration using Excel’s modern “Helper” functions.

The Minimal Working Example: Replacing Recursion with REDUCE

Section titled “The Minimal Working Example: Replacing Recursion with REDUCE”

The most common reason for hitting the recursion limit is trying to process a list item-by-item using a self-referencing function. Instead of calling a function inside itself, use REDUCE. It is designed to “loop” through an array without triggering the recursion counter.

Tested on: Excel for Microsoft 365 (Desktop and Web) as of May 2024.

Imagine you have a list of strings in column A, and you want to remove a list of “forbidden characters” (like @, #, $, %) from every string.

The “Broken” Recursive Way (Avoid for >250 items):

=LAMBDA(text, chars,
IF(chars="", text,
RecursiveClean(SUBSTITUTE(text, LEFT(chars, 1), ""), RIGHT(chars, LEN(chars)-1))
)
)

The “Fixed” Iterative Way (Use this instead):

=REDUCE(A2:A1000, {"@","#","$","%"}, LAMBDA(current_val, char_to_remove,
SUBSTITUTE(current_val, char_to_remove, "")
))

Why This Works: Understanding the Arguments

Section titled “Why This Works: Understanding the Arguments”

The REDUCE function iterates through a collection and applies a logic step to each element, passing the result to the next step. Because it is an internal iterator, it does not add to the call stack, effectively bypassing the #NUM! error.

Argument Role Explanation
initial_value The Data The array or starting value you want to transform (e.g., your 1,000 rows).
array The Logic Steps The list of values you want to apply to your data (e.g., characters to strip).
lambda The Action A function defined as LAMBDA(accumulator, value, [logic]).
accumulator The Progress The “running total” or the state of the data after the previous iteration.

If you need to process large arrays where each row requires complex logic that isn’t just a simple SUBSTITUTE, follow these steps to move your logic into the Name Manager:

  1. Copy your core logic formula.
  2. Go to Formulas > Name Manager > New.
  3. Name your function (e.g., SafeProcess).
  4. In the Refers to box, wrap your logic in MAP or REDUCE: =LAMBDA(data_range, MAP(data_range, LAMBDA(row, YOUR_COMPLEX_LOGIC_HERE)))
  5. Call it in your sheet: =SafeProcess(A2:A5000).

Note: MAP is generally used when you want to apply a function to every row independently. REDUCE is used when the result of one step depends on the previous step (like stripping multiple different characters).


What if I need to process more than 10,000 rows?

Section titled “What if I need to process more than 10,000 rows?”

While REDUCE and MAP bypass the recursion limit, they are still subject to Excel’s calculation engine limits and your computer’s RAM. If you are processing tens of thousands of rows with complex logic, the sheet may become sluggish. In these cases, it is often better to use Data > Get Data (Power Query), which is optimized for large-scale transformations.

Can I use this for conditional calculations across filtered rows?

Section titled “Can I use this for conditional calculations across filtered rows?”

Yes. If you wrap your array in the FILTER function before passing it to REDUCE, the formula will only iterate over the visible or matching rows. For example: =REDUCE(FILTER(A2:A1000, B2:B1000="Active"), ...)

Is there a way to automate this with Office Scripts or VBA?

Section titled “Is there a way to automate this with Office Scripts or VBA?”

If the array functions still feel too slow, Office Scripts (the modern successor to VBA for Excel Web/365) allows you to use standard JavaScript for loops or .map() methods. These run outside the cell calculation engine and do not have the same recursion or nesting constraints as formulas. To explore this, go to the Automate tab and select New Script.