A LAMBDA is a formula with a name. You test it in a cell, store it in the Name Manager, and after that every sheet in the workbook can call it. The steps below use a small text-cleanup function. The same clicks save the longer mortgage schedule.
You need Excel for Microsoft 365 or Excel 2024. Excel 2021 does not have LAMBDA.
Start with a formula that already works
Do not invent the name first. Put a messy value in A2: a couple of ordinary spaces, a non-breaking space between the words (the kind you get pasting from a web page), and a tab if you have one. This formula returns a single-spaced Jane Doe:
=TRIM(CLEAN(SUBSTITUTE(A2, CHAR(160), " ")))
CHAR(160) is the non-breaking space. CLEAN removes the characters 0 through 31, which includes a tab, and it does not remove character 160. That is why the SUBSTITUTE is there. TRIM then strips the ends and collapses the runs of spaces. If A2 does not change, you do not have a non-breaking space. Type =CHAR(160) in a cell and paste that character into A2.
Give it a name
- Select a blank cell. This matters, and the next section says why.
- On the Formulas tab, open Name Manager, then New. Define Name on the same tab is the same dialog.
- Name:
xl.text.clean. A name starts with a letter, an underscore, or a backslash. After that it may contain letters, numbers, periods, and underscores. It cannot contain a space, and it cannot be a cell address.X1is a cell.xl.text.cleanis not. - Scope: Workbook. A sheet-scoped name only works on that sheet, which is rarely what you want.
- Comment:
Trim spaces, tabs, and non-breaking spaces.The comment shows up as the function tooltip when you type the name. - Refers to:
=LAMBDA(text, TRIM(CLEAN(SUBSTITUTE(text, CHAR(160), " "))))
- OK. In a cell, type
=xl.text.clean(A2).
The argument text is the only input. There is no A2 inside the LAMBDA. The cell reference stays at the call, =xl.text.clean(A2), where it belongs.
The mistakes that waste an afternoon
A relative reference inside the name. If you had written =LAMBDA(text, TRIM(CLEAN(SUBSTITUTE(A2, CHAR(160), " ")))), the A2 would be stored relative to whichever cell was selected when you pressed OK. Move the formula and it points somewhere else. Pass every input as an argument.
#NAME?. The name is misspelled, or it was saved with sheet scope and you are on a different sheet. Names are not case-sensitive. XL.Text.Clean and xl.text.clean are the same name.
#VALUE!. The call has the wrong number of arguments, or the body returned something the cell cannot hold. xl.text.clean takes one argument.
#SPILL!. The LAMBDA returned an array, and the cells it needs are not empty. A single text result does not spill. The mortgage schedule does, five columns wide and one row per month. Clear that range before you blame the formula.
The name did not travel with a copy. Copying a cell that says =xl.text.clean(A2) into another workbook copies the call, not the definition. Copy the name in Name Manager, or save the file as a template and start from that.
Use it on a longer formula
Once the dialog is familiar, the mortgage schedule is the same procedure with a longer Refers to. Test a six-month loan in a cell first, the one on that page whose month-1 interest is exactly $12. Then paste the LAMBDA into Name Manager as xl.mortgage.schedule. A name is not a place to debug. The cell is.
US Excel separates arguments with commas. If Name Manager shows semicolons, the workbook’s locale uses semicolons, and you should type the formula the way that dialog already displays other functions. Do not mix the two in one name.