Purpose
Return value
Syntax
=LAMBDA(parameter,...,calculation)
- parameter - An input value for the function.
- calculation - The calculation to perform as the result of the function. Must be last argument.
How to use
The LAMBDA function provides a way to create a custom function in Excel. Once defined and named, a LAMBDA function can be used anywhere in a workbook. LAMBDA functions can be very simple or quite complex, stringing together many Excel functions into one formula. A custom LAMBDA function does not require VBA or macros.
Example 1 | Example 2 | Example 3 | More examples
In computer programming, the term "lambda" refers to an anonymous function or expression. An anonymous function is a function defined without a name. In Excel, the LAMBDA function is first used to create a generic (unnamed) formula. Once a generic version has been created and tested, it is ported to the Name Manager, where it is formally defined and named.
One of the key benefits of a custom LAMBDA function is that the logic contained in the formula exists in just one place. This means there is just one copy of code to update when fixing problems or updating functionality, and changes will automatically propagate to all instances of the LAMBDA function in a workbook.
The LET function is often used together with the LAMBDA function. LET provides a way to declare variables and assign values in a formula. This makes more complicated formulas easier to read by reducing redundant code. The LET function can also improve performance by reducing the number of calculations performed by a formula.
By default, all arguments in a LAMBDA function are required. To create optional arguments, see the ISOMITTED function.
Creating a LAMBDA function
LAMBDA functions are typically created and debugged in the formula bar on a worksheet, then moved into the name manager to assign a name that can be used anywhere in a workbook.
There are four basic steps to creating and using a custom LAMBDA function:
- Verify the logic you will use with a standard formula
- Create and test a generic (unnamed) LAMBDA version of the formula
- Name and define the LAMBDA formula with the name manager
- Call the new custom function with the defined name
The examples below discuss these steps in more detail.
Example 1 - basic example
To illustrate how LAMBDA works, let's begin with a very simple formula:
=x*y // multiply x and y
In Excel, this formula would typically use cell references like this:
=B5*C5 // with cell references
As you can see, the formula works fine, so we are ready to move on to creating a generic LAMBDA formula (unnamed version). The first thing to consider is if the formula requires inputs (parameters). In this case, the answer is "yes" – the formula requires a value for x, and a value for y. With that established, we start off with the LAMBDA function, then add the required parameters for user input:
=LAMBDA(x,y // begin with input parameters
Next, we need to add the actual calculation, x*y:
=LAMBDA(x,y,x*y)
If you enter the formula at this point, you'll get a #CALC! error. This happens because the formula has no input values to work with since there are no longer any cell references. To test the formula, we need to use a special syntax like this:
=LAMBDA(x,y,x*y)(B5,C5) // testing syntax
This syntax, where parameters are supplied at the end of a LAMBDA function in a separate set of parentheses, is unique to LAMBDA functions. This allows the formula to be tested directly on the worksheet before the LAMBDA is named. In the screen below, you can see that the generic LAMBDA function in F5 returns exactly the same result as the original formula in E5:
We are now ready to name the LAMBDA function with the Name Manager. First, copy the formula, but do not include the testing parameters at the end. Next, open the Name Manager with the shortcut Control + F3, and click New.
In the New Name dialog, enter the name "XBYY", leave the scope set to "Workbook", and paste the formula you copied into the "Refers to" input area. (Tip: Use the tab key to navigate to the "Refers to" field).
Make sure the formula begins with an equals sign (=). Now that the LAMBDA formula has a name, it can be used in the workbook like any other function. In the screen below, the formula in G5, copied down, is:
=XBYY(B5,C5)
The screen below shows how things look in the workbook:
The new custom function returns the same result as the other two formulas.
Example 2 - volume of sphere
In this example, we'll convert a formula to calculate the volume of a sphere into a custom LAMBDA function. The general Excel formula for calculating the volume of a sphere is:
=4/3*PI()*A1^3 // volume of sphere
where A1 represents the radius. The screen below shows this formula in action:
Notice this formula only requires one input (radius) to calculate volume, so our LAMBDA function will only need one parameter (r), which will appear as the first argument. Here is the formula converted to LAMBDA:
=LAMBDA(r,4/3*PI()*r^3) // generic lambda
Back in the worksheet, we've replaced the original formula with the generic LAMBDA version. Notice we are using the testing syntax, which allows us to plug in B5 for radius:
The results from the generic LAMBDA formula are exactly the same as the original formula, so the next step is to define and name this LAMBDA formula with the Name Manager, as explained above. The name used for a LAMBDA function can be any valid Excel name. In this case, we'll name the formula "SphereVolume".
Back in the worksheet, we've replaced the generic (unnamed) LAMBDA formula with the named LAMBDA version and entered B5 for r. Notice that the results returned by the custom SphereVolume function are exactly the same as those of previous results.
Example 3 - count words
In this example, we'll create a LAMBDA function to count words. Excel doesn't have a function for this purpose, but you can count words with a cell with a custom formula based on the LEN and SUBSTITUTE functions like this:
=LEN(TRIM(A1))-LEN(SUBSTITUTE(A1," ",""))+1
Read the detailed explanation here. Here is the formula in action in a worksheet:
Notice we get an incorrect count of 1 when the formula is given an empty cell (B10). We'll address this problem below.
This formula only requires one input: the text containing words. In our LAMBDA function, we'll name this argument "text". Here is the formula converted to LAMBDA:
=LAMBDA(text,LEN(TRIM(text))-LEN(SUBSTITUTE(text," ",""))+1)
Notice "text" appears as the first argument, and the calculation is the second and final argument. In the screen below, we've replaced the original formula with the generic LAMBDA version. Notice we are using the testing syntax, which allows us to plug in B5 for text:
=LAMBDA(text,LEN(TRIM(text))-LEN(SUBSTITUTE(text," ",""))+1)(B5)
The results from the generic LAMBDA formula are the same as the original formula, so the next step is to define and name this LAMBDA formula with the Name Manager, as explained previously. We'll name this formula "CountWords".
Below, we've replaced the generic (unnamed) LAMBDA formula with the named LAMBDA version, and entered B5 for text. Notice we get exactly the same results.
The formula used in the Name Manager to define CountWords is the same as above, without the testing syntax:
=LAMBDA(text,LEN(TRIM(text))-LEN(SUBSTITUTE(text," ",""))+1)
Fixing the empty cell problem
As mentioned above, the formula above returns an incorrect count of 1 when a cell is empty. This problem can be fixed by replacing +1 with the code below:
=LEN(TRIM(B5))-LEN(SUBSTITUTE(B5," ",""))+(LEN(TRIM(B5))>0)
Full explanation here. To update the existing named LAMBDA formula, we again need to use the Name Manager:
- Open the Name Manager
- Select the name "CountWords" and click "Edit"
- Replace the "Refers to" code with this formula:
=LAMBDA(text,LEN(TRIM(text))-LEN(SUBSTITUTE(text," ",""))+(LEN(TRIM(text))>0))
Once the Name Manager is closed, the CountWords works correctly on empty cells, as seen below:
Note: by updating the code once in the Name Manager, all instances of the CountWords formula are updated at once. This is a key benefit of custom functions created with LAMBDA –formula updates can be managed in one place.
LAMBDA naming rules
When naming a custom LAMBDA function in Excel, there are some restrictions you should be aware of:
- The name must be between 1 to 255 characters long.
- The name must start with a letter (A-Z, a-z) or an underscore (_).
- The name must not contain spaces or special characters like @, !, #, $, %, etc.
- The name must not be a cell reference like A1, C2, X100, etc.
- The name must not conflict with an existing Excel function like SUM, COUNT, TEXT, etc.
The last point, 5, is especially important. While Excel will stop you from breaking the first 4 rules, it will not prevent you from naming your custom LAMBDA after an existing function. If you break this rule, the name will be created, but your custom function will never be invoked since Excel will default to existing function names. The result can be very confusing. Avoid this trouble by making sure your custom function name is unique.