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.

Intermediate 20 min read

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

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.

D2fx=LET(net, B2*C2, vat, net*24%, net+vat)
ABCD
1ItemQtyUnit priceGross
2Paper (box)418.591.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:

Formula
=LET(
    pay, FILTER(C2:C7, B2:B7=H1),
    n,   COUNT(pay),
    IF(n=0, "No staff", ROUND(AVERAGE(pay), 0))
)
Press Alt+Enter (Mac: Option+Return) inside the formula bar to break a long formula into lines.

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.

Tip. Names in LET must start with a letter and cannot look like cell addresses. 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

Formula
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:

Formula
=LAMBDA(net, rate, net*(1+rate))(100, 24%)
Returns 124. The trailing (100, 24%) calls the function once, for testing.

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.

  1. 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.
  2. Copy the formula from the formula bar, without the test call (31500) at the end.
  3. Open Formulas › Defined Names › Name Manager and click New.
  4. 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.
  5. Paste the LAMBDA into Refers to and click OK.
  6. In any cell type =COMMISSION(B2). AutoComplete lists your function like any other.
C3fx=COMMISSION(B3)
ABC
1RepSalesCommission
2Anna18400736
3Bruno315001890
4Dina520004160

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.

Tip. Combine LET and LAMBDA for complex functions. And add optional parameters with square brackets plus ISOMITTED: =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.

FunctionWhat it doesExample
BYROWApplies a LAMBDA to each row, returns one result per row=BYROW(B2:D4, LAMBDA(r, MAX(r)))
BYCOLSame, per column=BYCOL(B2:D4, LAMBDA(c, AVERAGE(c)))
MAPApplies a LAMBDA to each item of one or more arrays=MAP(B2:B4, C2:C4, LAMBDA(q, p, q*p))
REDUCEAccumulates a single result across an array=REDUCE(0, B2:B4, LAMBDA(acc, x, acc+x))
SCANLike REDUCE but returns every intermediate step (running totals)=SCAN(0, B2:B4, LAMBDA(acc, x, acc+x))
MAKEARRAYBuilds an array from row and column numbers=MAKEARRAY(3, 3, LAMBDA(r, c, r*c))
E2fx=BYROW(B2:D4, LAMBDA(r, MAX(r)))
ABCDE
1StudentTest 1Test 2Test 3Best
2Ana78859191
3Ben64705870
4Carla92889595

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.
Watch out. LAMBDA formulas are not checked when you save the name. A typo inside the definition shows up only when the function is used. Always test with the inline call (…) before saving it to Name Manager.

Check yourself

1. What does =LET(x, 5, y, x*2, x+y) return?

2. What does a LAMBDA without a test call return when typed in a cell?

3. Where do you store a LAMBDA to use it as a custom function?

4. Which helper function returns running totals?

5. What is one benefit of LET besides readability?

⬇Practice workbookExamples with live formulas + practice sheets · .xlsx · 16 KB