LAMBDA · Text
Count words LAMBDA
xl.count.words counts words after trimming. Maria Elena Lopez is 3. A blank cell is 0.
Syntax =xl.count.words(text)
=LAMBDA(text,
LET(
cleaned, TRIM(text),
IF(LEN(cleaned)=0, 0, LEN(cleaned) - LEN(SUBSTITUTE(cleaned, " ", "")) + 1)
)
)Arguments
| Name | Required | What to pass |
|---|---|---|
| text | Required | The phrase to count. Extra spaces are collapsed by TRIM before the count. |
Examples
A three-word name
=xl.count.words("Maria Elena Lopez")Result 3
| A | B | |
|---|---|---|
| 1 | Text | Maria Elena Lopez |
| 2 | Words | 3 |
xl.count.words is the space count people rebuild by hand. Maria Elena Lopez has two spaces and three words. A cell that is empty, or only spaces, is 0 rather than 1.
TRIM collapses repeated spaces first, so Maria Lopez is still two words.
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.count.words, 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.count.words(...).
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
After TRIM, the number of words is the number of remaining spaces plus one. SUBSTITUTE removes the spaces and the length difference is the space count. An empty result short-circuits to 0, because the plus-one rule would otherwise call an empty cell one word.
Limits
Line breaks are not spaces. A manual line break inside a cell is not a word boundary unless you replace CHAR(10) with a space first. Hyphens do not split words.