Use BYROW and SCAN:

=BYROW(A1:A40,LAMBDA(c,SUM(SCAN(c,SEQUENCE(,4,6,0),LAMBDA(a,b,IF(a=1,0,ROUNDUP(a/b,0)))))))

Answer from Scott Craner on Stack Overflow
🌐
Ablebits
ablebits.com › ablebits blog › excel › lambda › recursive lambda function in excel with examples
Recursive LAMBDA function in Excel with examples
March 13, 2023 - Basically, a recursive function works by iteration and finds a solution to a bigger problem by solving smaller instances of the same problem. Currently, LAMBDA is the only Excel function that supports recursion, enabling you to create compact ...
🌐
Reddit
reddit.com › r/excel › defining recursive lambda functions inside of a let() function using fixed-point combinators
r/excel on Reddit: Defining recursive lambda functions inside of a LET() function using fixed-point combinators
November 18, 2021 -

I don't know if the rest of you love lambdas as much as I do but I have been using them extensively for the past few months and would like to share one of the tricks I have learned.

First lets start off with covering the simplest way of implementing recursive lambdas (which appears to be the method that Microsoft wants us to always use), the name manager... if we wanted to define a simple factorial function F() we would create a new name 'F' and put the following in it =LAMBDA(X,IF(X>1,X*F(X-1),1)) as you can see recursion works the way we would expect it to in 99% of programming languages; the function simply calls itself by name inside of its definition. For every sane person this is enough and they will define all of their recursive lambdas this way... but I had to see how far I could push it...

My dilemma was that I had a complicated operation that required helper functions, but I didn't want my namespace cluttered up with these helper functions and I didn't want users to be able to call them as they should only be called inside of my lambda function. My first instinct was to use the LET() function and it worked exactly as I wanted... but only for non recursive helper functions; for example if I wanted to define a function that rotated a matrix by 180 degrees I could do something like this:

=LAMBDA(Mat,
  LET(EM, LAMBDA(n, MAKEARRAY(n, n, LAMBDA(i, j, --(j=(n-i+1)))),
    MMULT(MMULT(EM(COLUMNS(Mat)),Mat),EM(ROWS(Mat)))
  )
)

The Lambda takes in a matrix 'Mat' and then multiplies it by 2 exchange matrices, we define the lambda EM to create an exchange matrix of arbitrary size n, then call it twice in our final Lambda definition which simply performs 2 matrix multiplications.

But what are we to do if we want to define a recursive lambda outside of the name manager?
As an example lets simply try defining and applying a recursive lambda inside of a let function... we will use the factorial definition from above:

=LET(
  F,LAMBDA(X,IF(X>1,X*F(X-1),1)),
  F(5)
)

When trying the formula above I always got a #NAME? Error because when evaluating the definition of 'F' the value of 'F' is not yet defined. This is similar to the problem encountered by C compilers when you call a function before it is defined. In C you can solve this issue with function prototypes (usually in a header file) which act as a 'temporary definition' of a function telling the compiler the input(s) & their type(s) and return type until the real definition happens later on in the code, but because the evaluation strategy of LET() is strict the definition of any name is immutable so we need to define it fully the first time.

For the past few months I thought it was impossible to work around this limitation but I finally figured it out, it turns out that all the time I spent learning lambda calculus was not pointless.

In lambda calculus a lambda expression can not refer to itself by name in its own definition, this is similar to the constraints of defining names in the Let() function. We can get around this limitation through the use of a fixed point combinator, the most famous of which is Curry's Y Combinator [Y := λg. (λx. g (x x)) (λx. g (x x))] but to solve this problem we need to use the Turing fixed-point combinator (also known as the theta combinator) [Θ := (λxg. g (x x g)) (λxg. g (x x g))] translated from lambda calculus to Excel formulas it works like this:

=LET(
  F,LAMBDA(G,X,IF(X>1,X*G(G,X-1),1)),
  F(F,5)
)

By applying the logic of the combinator to our lambda function we are no longer calling the function F inside of the definition of F, but instead calling the function G, which is passed to F as a parameter. when we finally want to call our 'recursive function F, we also pass the function to itself as its first parameter.
If you want to make your final formula more readable you can also curry the lambda to make it callable without having to pass itself as a parameter, but this does incur some minor extra overhead because you have to wrap it up in another lambda expression:

=LET(
  Y,LAMBDA(G,X,IF(X>1,X*G(G,X-1),1)),
  F,LAMBDA(X,Y(Y,X)),
  F(5)
)

--

Top answer
1 of 5
7
This is really cool, thanks! To save other people some debugging time , "...the current recursion limit is set as 1,024 divided by (number of lambda parameters + 1)", and formulas will display a very unhelpful "#NUM" error when you reach the recursion limit.
2 of 5
4
Hey, I started playing around with Excel recursive lambdas and in a search for some ideas I happened upon this post. I have absolutely devoured this and have been playing around with your Y Combinator idea for days now. I've used it to develop a number of reusable recursion functions, chiefly a "SUPERBYROW" and "SUPERBYCOL" function that imitates BYROW and BYCOL but allows the results of each row/col to be arrays or matrices of any dimension, whereas the original functions allow only scalar return. But these functions further planted the seed to variations that allow generalized Do While and Do Until loops. In fact one could create a "do until input condition" but also a "do until result condition." Your post has been utterly inspiring and I wanted to thank you personally for that. I've watched videos and read articles on recursive Lambdas but there's something about your post that lit the fuse. I feel like you opened up a door to a universe that I'm not entirely sure I should have opened, this could be a consuming rabbit hole! So, I did what all good Redditors do, I cyber-stalked you on Reddit to see if you have published any other interesting thoughts on the topic. :) And in so doing I discovered your diagnosis and situation. :( I wish you all the best, and if you ever need to nerd out and talk recursive lambdas on a zoom call, my DMs here are always open to you.
Discussions

recursion - How to write a LAMBDA function in Excel for this recursive calculation - Stack Overflow
I'm trying to come up with a LAMBDA formula that captures the following recursive calculation: Column A has 40 rows with integers between 1 and 40. Column B divides each integer in column A by 6 and More on stackoverflow.com
🌐 stackoverflow.com
Lambda to generate a recursive list while retaining all values?
It’s a good idea to have a basic understanding of this amazing function. This way, you can speak about LAMBDA functions with authority at your next Excel party. (You know those parties are amazing!) The most impactful aspect of the LAMBDA function is that it can be used recursively. More on learn.microsoft.com
🌐 learn.microsoft.com
5
2
July 14, 2022
Use new LAMBDA functions recursively?
r/excel: Discuss and answer questions about Microsoft Office Excel and spreadsheets in general More on amp.reddit.com
🌐 r/excel
48
December 10, 2020
How do I make a recursive function on excel?
You could do this with a WHILE loop. More on reddit.com
🌐 r/excel
10
5
December 6, 2019
🌐
My Online Training Hub
myonlinetraininghub.com › home › blog › recursive lambda functions
Recursive LAMBDA Functions • My Online Training Hub
September 1, 2023 - Recursive LAMBDA Functions further extend the formula functionality in Excel. In this tutorial see how to write and evaluate them in a cell.
🌐
Exceljet
exceljet.net › lambda replace characters recursive
LAMBDA replace characters recursive - Excel formula | Exceljet
May 13, 2024 - The LAMBDA function can be used to create a custom function to replace characters. In the example shown, cell C5 contains the custom LAMBDA function "ReplaceChars": =ReplaceChars(B5,"!@#$%^&*()[] -?.,","") The ReplaceChars function is designed ...
🌐
XelPlus
xelplus.com › home › tutorials › how a recursive lambda works
How a Recursive LAMBDA Works - Xelplus - Leila Gharani
March 24, 2023 - The way this works is that the LAMBDA calls itself as many times as is needed depending on the length of the “Before/After” list. This is known as a Recursive Function. If you were to try this type of trickery with a traditional Excel function, ...
Find elsewhere
🌐
SpreadsheetWeb
spreadsheetweb.com › home › how to create recursive functions in excel with lambda
How to create recursive functions in Excel with LAMBDA
April 29, 2021 - It is accepted one of the simplest examples of a function that is computable but not primitive recursive. ... If m and n values are not equal to 0 (third line), the function calls itself to calculate its second argument. The called function will call another, until the conditions in the first two rows are met. The following is the Excel version of the Ackermann Function. We named it “Ack”. =LAMBDA(m,n,IF(m=0,n+1,IF(n=0,Ack(m-1,1),Ack(m-1,Ack(m,n-1)))))
🌐
FM Magazine
fm-magazine.com › home › news › working with me to test excel’s new lambda function
Working with ME to test Excel’s new LAMBDA function - FM
March 23, 2021 - As you may recall, LAMBDA allows ... “CUSTOM1”). Once you’ve created the custom function, you can reuse it anywhere in your Excel workbook simply by referring to its name (eg, “CUSTOM1”). LAMBDA “completes” Excel because ...
🌐
YouTube
youtube.com › watch
Excel RECURSIVE Lambda - Create loops with ZERO coding! - YouTube
Join 400,000+ professionals in our courses here 👉 https://link.xelplus.com/yt-d-all-coursesThe new Excel LAMBDA Function allows you to create your own CUSTO...
Published   December 17, 2020
🌐
Microsoft Community
techcommunity.microsoft.com › microsoft community hub › communities › products › microsoft 365 › excel
Recursive LAMBDA implementation of Excel's REDUCE function. | Microsoft Community Hub
Out of curiosity, I decided to develop an implementation of Excel's REDUCE function as a recursive Excel LAMDA function. I was truly amazed how simple it was to write such a powerful function. =LAMBDA(initial_value,array,CLAMBDA, LET( _00,"Implementation of REDUCE in Excel Lambda", vec, TOROW(array), rec_L, LAMBDA(acc,cindex,rec_LL, LET( cvalue, INDEX( vec,0,cindex ), new_acc, CLAMBDA( acc, cvalue ), IF( cindex = COLUMNS(vec), new_acc, rec_LL(new_acc,cindex+1,rec_LL ) ) ) ), rec_L( initial_value,1,rec_L) ) )(0, {1,2,3}, LAMBDA(acc,cv, acc+cv))
🌐
SumProduct
sumproduct.com › news › article › lambda-formulaic-recursion-its-all-about-me
LAMBDA Formulaic Recursion: It’s All About ME! – SumProduct
May 1, 2021 - In Office 365 Beta, LAMBDA allows you to define a custom function in Excel’s very own formula language. Moreover, one prescribed function may call another. If the function calls itself, that’s an example of recursion, which is a way of a function calling itself, called recursion, which is ...
🌐
Sheetaki
sheetaki.com › home › how to write a recursive lambda function in excel
How to Write Recursive LAMBDA Function in Excel - Sheetaki
January 25, 2023 - Let’s take a look at a real example of the LAMBDA function being used in an Excel spreadsheet. In the table below, we created a custom recursive function to clean text. Users simply need to add the characters to remove in cell D2.
🌐
Exceldashboardschool
exceldashboardschool.com › home › excel lambda function › recursive lambda function in excel with examples
Recursive LAMBDA Function in Excel with Examples
August 13, 2025 - Don’t panic, recursive formulas with the LAMBDA function are much simpler than they sound. This guide shows you how to use modern Excel functions like LET, MAP, TEXTJOIN, and FILTER to build compact recursive logic, with no VBA required.
🌐
Trump Excel
trumpexcel.com › home › excel functions › lambda function
Excel LAMBDA Function - Easy Explanation with Examples
Recursive Lambda is an advanced concept that allows you to create a lambda function and then call the same function from within itself.
Published   January 30, 2026
🌐
Microsoft Community
techcommunity.microsoft.com › microsoft community hub › communities › products › microsoft 365 › excel
HRECURSE instead of MAKEARRAY, recursing LAMBDA | Microsoft Community Hub
June 30, 2022 - This function only takes one input - the row within the previous result vector - and we are now able to rephrase the proposition above as: =HRECURSE(1;5;{1;2};LAMBDA(i;p;VSTACK(p(1)+1;p(2)*2))) I propose that this notation is generic enough for anyone to SPILL contextual series calculation without requiring a profound grasp of recursion. PS: Regrettably, Excel will not allow me to make the row input into p() optional.
🌐
GitHub
gist.github.com › snth › 03a3622f5eaab58fb120520cf3d0e91e
RECURSIVE♾ - Excel LAMBDA function for recursion in Excel
Great, so we've seen that we can do recursion in Excel but can we do anything actually useful with it? Just this week one of our Portfolio Managers asked me to assist with a portfolio optimisation problem. While I used the Excel Solver Add-in for that, objective function maximisation and root finding are a nice application so let's explore that. Over the next two weeks, the festive "spirits" of many people can be approximated by the following XMAS function: XMAS = LAMBDA(d, 1 + MAX(-1, -(d - 45651.5) * (d - 45651.5) * (d - 45658) * (d - 45658) / 200));
🌐
Microsoft Support
support.microsoft.com › en-us › office › lambda-function-bd212d27-1cd1-4321-a34a-ccbf254b8b67
LAMBDA function - Microsoft Support
If you call a LAMBDA function from within itself and the call is circular, Excel can return a #NUM! error if there are too many recursive calls.
🌐
MrExcel
mrexcel.com › forums › other forums › mrexcel excel videos
Solving Slugify With Recursive Lambdas In Excel - 2390 | MrExcel Message Board
March 2, 2021 - Building a SLUGIFY function in Excel using the brand new recursive LAMBDA function. This video shows how to use an Index argument to act as a loop counter in an Excel formula. Thanks to Smozgur who posted this example at the MrExcel Message Board LAMBDA forum: Excel LAMBDA Functions The...