Excel LAMBDA Function
The Excel LAMBDA function provides a way create custom functions that can be reused throughout a workbook, without VBA or macros.
- parameter - An input value for the function.
- calculation - The calculation to perform as the result of the function. Must be last argument.
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.
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.
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 use 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, and add the required parameters for user input:
=LAMBDA(x,y // begin with input parameters
Next, we need to add the actual calculation, 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, not including 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:
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 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:
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 the results returned by the custom SphereVolume function are exactly the same as 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:
Read the detailed explanation here. Here is the formula in action in a worksheet:
Notice we are getting 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, which is the text that contains words. In our LAMBDA function, we'll name this argument "text". Here is the formula converted to LAMBDA:
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:
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:
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:
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:
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.