Recursive LAMBDA

Excel Formulas › Advanced

365 / 2021LAMBDA

A LAMBDA can call itself — once you name it — to loop without VBA. Recursion handles tasks like cleaning a list of characters, walking a tree, or repeating an operation a variable number of times.


Quick formula: name a LAMBDA in Name Manager, then have it call its own name:
RemoveChars = LAMBDA(txt, chars, IF(chars="", txt, RemoveChars(SUBSTITUTE(txt, LEFT(chars,1), ""), MID(chars,2,99))))
Each call strips one character and calls itself with the rest, until chars is empty — the base case that stops the recursion.

Functions used (tap for the full reference guide):

The example

Strip several unwanted characters in one named function.

AB
1InputCleaned
2(555) 123-45675551234567

The formula

A LAMBDA that calls itself:

RemoveChars = LAMBDA(txt, chars, IF(chars="", txt, RemoveChars(SUBSTITUTE(txt, LEFT(chars,1), ""), MID(chars,2,99)))) // recurses until chars is empty

How it works

Recursion needs a name and a base case:

  1. Define the LAMBDA in Name Manager with a name (e.g. RemoveChars) — recursion needs the name to refer to itself.
  2. Include a base case: IF(chars="", txt, …) stops when there’s nothing left to do.
  3. In the recursive branch, do one step and call the name again with reduced input.
  4. Call it on the sheet like any function: =RemoveChars(A2, "()- ").

Always give it a base case. Without a stopping condition, a recursive LAMBDA loops until Excel runs out of memory and returns an error. Test on small inputs first, and watch performance on long lists — sometimes REDUCE or SCAN is a cleaner loop.

Try it: interactive demo

Live demo

Strip these characters from the text.

Result:

Variations

Factorial

Classic recursion:

Fact = LAMBDA(n, IF(n<=1, 1, n*Fact(n-1)))

Repeat N times

Loop a transform:

Rep = LAMBDA(x,n, IF(n=0, x, Rep(step(x), n-1)))

REDUCE instead

Often simpler than recursion:

=REDUCE(txt, chars, LAMBDA(a,c, SUBSTITUTE(a,c,"")))

Pitfalls & errors

Must be named. A LAMBDA can only recurse if it has a name to call — an anonymous inline LAMBDA can’t refer to itself.

Base case is mandatory. No stopping condition = infinite recursion = error.

Watch performance. Deep recursion over long inputs can be slow; consider REDUCE/SCAN for list loops.

Practice workbook

📊
Download the free Recursive LAMBDA practice workbook
A recursive LAMBDA example (formula text + result) with factorial and REDUCE variants, plus 4 challenges with answers. No sign-up required.

Frequently asked questions

How do I make a recursive LAMBDA in Excel?
Define the LAMBDA in Name Manager so it has a name, include a base case (an IF that stops), and call the name inside itself with reduced input. Then use it on the sheet like a function.
Why does my recursive LAMBDA error?
Either it has no base case (infinite recursion) or it isn't named — an anonymous LAMBDA can't call itself. Add a stopping IF and define it in Name Manager.
Is recursion the only way to loop in Excel?
No — REDUCE and SCAN loop over a list without recursion and are often simpler and faster for that case.

Stop fighting formulas. Learn them in a day.

This recipe is one of hundreds of real-world formulas we teach. Our Excel Formulas & Functions class covers lookups, logic, text, and dynamic arrays hands-on — live in Dallas–Fort Worth, Houston, Austin, Oklahoma City, Denver, or online.

See the Formulas & Functions Class

Related formulas: LAMBDA function · REDUCE function · Named LAMBDA function

Function references: LAMBDA