Excel course · Lesson 22 of 34
LET and LAMBDA: variables and your own functions
Name parts of a formula with LET, build reusable custom functions with LAMBDA and Name Manager, and use BYROW, MAP and REDUCE helpers.
As formulas grow, they start repeating themselves. The same sub-calculation appears three times, and a reader has to untangle a wall of parentheses. LET fixes this by letting you give names to values inside a formula. LAMBDA goes one step further: it lets you turn a formula into your own reusable function, with a name, arguments and no VBA. Both are available in Microsoft 365; LET also in Excel 2021, and LAMBDA in Excel 2024.
LET: variables inside a formula
LET(name1, value1, [name2, value2, ...], calculation)You list pairs of a name and its value, and finish with a calculation that uses those names. The names exist only inside this formula.
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Item | Qty | Unit price | Gross |
| 2 | Paper (box) | 4 | 18.5 | 91.76 |
net = 74, vat = 17.76, result = 91.76.
Result: 91.76
Read it like a short recipe: let net be quantity times price; let vat be 24% of net; return net plus vat. Without LET you would write =B2*C2+B2*C2*24%, which repeats the multiplication and hides the meaning.
Why LET matters
- Readability: names explain each step.
- Speed: a value defined once is calculated once. If a slow FILTER or XLOOKUP is used three times in a formula, LET computes it only once.
- Fewer mistakes: change a sub-calculation in one place instead of three.
- Debugging: temporarily make the final argument one of the names, for example
vat, to see that intermediate value.
A realistic LET formula
Show the average salary of a department chosen in H1, or a message if there is nobody in it:
=LET(
pay, FILTER(C2:C7, B2:B7=H1),
n, COUNT(pay),
IF(n=0, "No staff", ROUND(AVERAGE(pay), 0))
)With the staff list from Dynamic arrays: spill, FILTER, SORT, UNIQUE, SEQUENCE and # and H1 = IT, this returns 59,750: the average of 61,200 and 58,300. Note that FILTER returns #CALC! when nothing matches, and COUNT of an error is 0, so the IF catches the empty case.
rate1 or tax are fine; r1 and tax2026 are not, because R1 and TAX2026 are cells. Use underscores (tax_2026).LAMBDA: build your own function
LAMBDA([parameter1, parameter2, ...], calculation)A LAMBDA defines parameters and a calculation. On its own in a cell it returns #CALC!, because nothing has been passed in yet. You can test it by adding the arguments in parentheses right after it:
=LAMBDA(net, rate, net*(1+rate))(100, 24%)Turning a LAMBDA into a named function
The real power comes when you store a LAMBDA under a name. It then works like a built-in function everywhere in the workbook. Example: a sales commission with three tiers.
- Write and test the logic in a cell first:
=LAMBDA(sales, IF(sales>=50000, sales*8%, IF(sales>=25000, sales*6%, sales*4%)))(31500). It returns 1,890. - Copy the formula from the formula bar, without the test call
(31500)at the end. - Open Formulas › Defined Names › Name Manager and click New.
- Name:
COMMISSION. Scope: Workbook. Comment:Tiered commission: 4% / 6% from 25k / 8% from 50k. The comment appears as a tooltip when you type the function. - Paste the LAMBDA into Refers to and click OK.
- In any cell type
=COMMISSION(B2). AutoComplete lists your function like any other.
| A | B | C | |
|---|---|---|---|
| 1 | Rep | Sales | Commission |
| 2 | Anna | 18400 | 736 |
| 3 | Bruno | 31500 | 1890 |
| 4 | Dina | 52000 | 4160 |
C2:C4 use the custom COMMISSION function: 18,400×4%, 31,500×6%, 52,000×8%.
Result: 1890
When the commission policy changes, you edit the name once in Name Manager and every cell that uses COMMISSION updates. That is the main reason to use LAMBDA: business rules live in one place.
=LAMBDA(net, [rate], net*(1+IF(ISOMITTED(rate), 24%, rate))) defaults the rate to 24%.LAMBDA helper functions
A family of functions takes a LAMBDA as an argument and applies it to arrays. They let one formula do row-by-row or item-by-item work that used to need helper columns.
| Function | What it does | Example |
|---|---|---|
| BYROW | Applies a LAMBDA to each row, returns one result per row | =BYROW(B2:D4, LAMBDA(r, MAX(r))) |
| BYCOL | Same, per column | =BYCOL(B2:D4, LAMBDA(c, AVERAGE(c))) |
| MAP | Applies a LAMBDA to each item of one or more arrays | =MAP(B2:B4, C2:C4, LAMBDA(q, p, q*p)) |
| REDUCE | Accumulates a single result across an array | =REDUCE(0, B2:B4, LAMBDA(acc, x, acc+x)) |
| SCAN | Like REDUCE but returns every intermediate step (running totals) | =SCAN(0, B2:B4, LAMBDA(acc, x, acc+x)) |
| MAKEARRAY | Builds an array from row and column numbers | =MAKEARRAY(3, 3, LAMBDA(r, c, r*c)) |
| A | B | C | D | E | |
|---|---|---|---|---|---|
| 1 | Student | Test 1 | Test 2 | Test 3 | Best |
| 2 | Ana | 78 | 85 | 91 | 91 |
| 3 | Ben | 64 | 70 | 58 | 70 |
| 4 | Carla | 92 | 88 | 95 | 95 |
One formula in E2 returns each student's best score.
You might ask why not just write =MAX(B2:D2) and fill down. For a fixed table that is fine. BYROW shines when the input is itself a dynamic result, such as the output of FILTER, because the answer spills and resizes with it. Helper functions also accept the name of a named LAMBDA directly: =MAP(B2:B4, COMMISSION).
Recursion, briefly
A named LAMBDA may call itself, which allows loops. For example a function that removes a list of unwanted characters one by one. Recursion is powerful but easy to get wrong; most needs are now covered by REDUCE and the other helpers, which are easier to read.
Sharing and maintaining LAMBDA functions
- Named LAMBDAs live in the workbook. To use them in another file, copy a sheet that uses them into the new file, or copy the definitions via Name Manager.
- Document each function in the Name Manager comment: what it does, its parameters, and an example.
- Microsoft's free Advanced Formula Environment add-in (part of Excel Labs) gives a proper editor with indentation and comments for long LAMBDAs.
- Colleagues on Excel 2021 or older cannot calculate LAMBDA formulas; they see
#NAME?. Check versions before relying on them. - For logic that needs files, loops over sheets or user interface, VBA is still the tool: see What is VBA? Macros, when to use them and macro security.
(…) before saving it to Name Manager.Check yourself
1. What does =LET(x, 5, y, x*2, x+y) return?
x = 5, y = 10, so x + y = 15.
2. What does a LAMBDA without a test call return when typed in a cell?
A LAMBDA needs arguments; on its own it returns #CALC!.
3. Where do you store a LAMBDA to use it as a custom function?
A defined name that refers to a LAMBDA becomes a workbook function.
4. Which helper function returns running totals?
SCAN returns every intermediate accumulator value; REDUCE returns only the final one.
5. What is one benefit of LET besides readability?
LET evaluates each named value once, which can make formulas faster.