LAMBDA · Text
Collapse spaces LAMBDA
xl.collapse.space turns nonbreaking spaces into ordinary spaces and trims the result. A padded city becomes Austin.
Syntax =xl.collapse.space(text)
=LAMBDA(text,
LET(
ordinary, SUBSTITUTE(text, CHAR(160), " "),
TRIM(ordinary)
)
)Arguments
| Name | Required | What to pass |
|---|---|---|
| text | Required | Text copied from a web page or a PDF, where blank-looking characters may be nonbreaking spaces. |
Examples
A city with ordinary extra spaces
=xl.collapse.space(" Austin ")Result Austin
| A | B | |
|---|---|---|
| 1 | Raw | Austin |
| 2 | Clean | Austin |
xl.collapse.space is TRIM that also understands the nonbreaking space web pages use. Character 160 looks blank and survives TRIM. This function turns it into a normal space first, then TRIM collapses the runs and strips the ends. Austin becomes Austin.
Line breaks are a different character. CLEAN is the function for those.
Add it to the workbook
Copy the formula in the block above, including the line breaks. Choose Formulas, Name Manager, New. Set the name to xl.collapse.space, the scope to Workbook, and paste into Refers to. Excel Labs can do the same step: open the Advanced Formula Environment, add that name, and save it back to the workbook. A cell then calls =xl.collapse.space(...).
The name needs Microsoft 365, Excel 2024, or Excel for the web. Excel 2021 returns #NAME? for LAMBDA even when it already has XLOOKUP, FILTER, or LET.
How it works
SUBSTITUTE replaces every CHAR(160) with a space. TRIM then removes leading and trailing spaces and reduces internal runs to one space.
Limits
Other Unicode spaces, such as a thin space, are not character 160 and are not changed. The function does not change case and does not remove punctuation.