Excel Function to Clear Empty Strings: Text Cleaning and Character Counting

Explain how to clear empty strings and remove invisible characters in Excel, and how to count characters and non-empty cells. The article gives formula combinations, hands-on steps, and item-by-item verification methods, applicable to any version of spreadsheet software.

Key Points of This Article

  • A truly empty cell is not the same as a cell returning an empty string from a formula, so distinguish them before counting and filtering, or the totals will not match.
  • Combining TRIM, CLEAN, and SUBSTITUTE removes extra spaces, non-printing characters, and non-breaking spaces.
  • LEN counts the characters in a single cell, and SUMPRODUCT with LEN can total the characters in an entire column.
  • The "?*" criterion in COUNTIF counts only text cells containing at least one character, which suits counting rows that truly have content.
  • Keep the original column before cleaning, use LEN before and after to check how many characters were removed, and return to the source data if the results are in doubt.

Many people working in Excel to organize customer lists, product codes, or survey answers have run into similar problems: cells that look empty get counted in totals, and two columns that look identical fail to match in formulas. Most of these issues come from empty strings, leading and trailing spaces, and invisible control characters. Readers who search for "Excel function to clear empty strings" usually need a set of cleaning and counting methods they can apply directly.

This article covers only two things: how to remove empty strings and invisible characters, and how to count characters and non-empty cells. The basic usage for removing extra spaces will not be expanded on here; instead, it will be placed within a complete data-cleaning workflow. The methods are based on general Excel functions, and the usage of functions with the same names in other spreadsheet software is largely the same.

Excel formula results do not change from one spreadsheet program to another, so the methods below can be reproduced in any spreadsheet that supports these functions. If readers need to hand the cleaned table to someone else, they can open it in the UniCell spreadsheet in the OmniDoc desktop client to check it.

Removing empty strings or removing whitespace? The one-sentence answer

First identify which kind of "blank" the data actually contains, then choose the function: empty strings returned by formulas are removed with conditional tests or filtering, leading/trailing and internal spaces are handled with TRIM, and invisible control characters are handled with CLEAN. Combining these three is generally enough.

Your situationRecommended PracticesCause
A cell looks blank, but its formula returns "" or the data comes from an exported fileUse LEN(A1)=0 to check, or use FILTER to remove rows whose length is 0LEN can recognize both empty-string values and NULL values, so the judgment criteria are unified.
Extra spaces at the beginning, end, or in the middle of the text.Clean with TRIM, don't just use SUBSTITUTE to remove all spacesTRIM keeps a single space between words to avoid running English words together.
Invisible characters appear after copying from a web page or other systemClean with SUBSTITUTE to handle CHAR(160)CLEAN removes non-printable control characters, and CHAR(160) is a common non-breaking space.
I need to count the cells that contain content.COUNTIF with the "?*" criterionCount only text that contains at least one character; do not count empty strings.
I need to count all the charactersSUMPRODUCT combined with LENLEN counts characters cell by cell, and SUMPRODUCT adds up the results.

Before You Begin: Confirm These Points

First, distinguish truly empty cells from empty strings. A truly empty cell has never had anything entered in it; an empty string is either a formula returning "" or a blank text value left behind by a data import. COUNTBLANK counts both, whereas COUNTA treats an empty string as non-empty, so the two give different results. Be clear about which definition you are using before you count.

Second, confirm whether the data is text or numbers. LEN counts numbers as strings too; for example, the LEN of the number 12345 is 5. If a column of IDs mixes numbers and text, the "?*" criterion in COUNTIF only counts the text entries, so the result will be smaller than COUNTA's, which is normal.

Third, keep the original columns. When cleaning, try to create new columns rather than overwriting the raw data directly. Keeping the original columns lets you use LEN before and after to check which characters were removed, and lets you trace back if something goes wrong.

Fourth, pay attention to the Excel version. The FILTER function is only available in newer versions. If you are using an older version, you can use the spreadsheet software's built-in filter feature to manually hide or delete the rows with a length of 0. The result is the same, except that it will not update automatically.

Fifth, watch out for differences with LENB. In a Chinese-language Excel environment, LENB counts bytes, and Chinese characters are usually counted as 2. To count characters, use LEN, unless you actually need the byte count.

Steps to Clean Text and Count Characters in Excel

  1. In a new column (e.g., column B), enter the cleaning formula in the first row: =TRIM(CLEAN(SUBSTITUTE(A1," "," "))). Here the second argument should be CHAR(160), i.e., =TRIM(CLEAN(SUBSTITUTE(A1,CHAR(160)," "))). This step removes non-breaking spaces, invisible control characters, and extra spaces.
  2. Fill the formula down to the last row of data, and check whether column B matches your expectations.
  3. If you want to replace empty strings with placeholder text, enter =IF(LEN(B1)=0,"无",B1) in column C, so that empty values are clearly marked instead of left as blank cells.
  4. In column D, calculate the number of characters in each cell: =LEN(B1). This column can be used to screen for abnormal lengths, such as identifiers longer than expected.
  5. In an empty cell, count the non-empty rows after cleaning: =COUNTIF(B:B,"?*"). This result only includes text cells with at least one character.
  6. Count the total number of characters in the whole column: =SUMPRODUCT(LEN(B1:B1000)). Adjust the range to the actual number of data rows.
  7. If you need to extract rows with empty strings separately, enter =FILTER(A1:A1000,LEN(A1:A1000)>0) in a new area to keep only non-empty results; the original data is not affected.
  8. Use =LEN(A1)-LEN(B1) to calculate the number of characters removed from each row, and confirm that the cleaning did not accidentally delete useful content.

Verify item by item after completion

After cleaning and counting are complete, don't use the results directly in reports. It's recommended that you check them against the following items.

Inspection itemHow do you see it?What should I do if I have a problem?
Is the number of non-empty lines reasonable?Compare the COUNTIF result with the number of visually filtered rows that contain contentIf the difference is large, check whether numeric content has been excluded, or whether there are cells containing only whitespace.
Character difference before and after cleaningSpot-check the rows where LEN(A1)-LEN(B1) is not 0Open the original cell to check whether any necessary symbols were accidentally deleted, and adjust the replacement rules if necessary.
Have the non-breaking spaces been fully cleaned up?Search for spaces in Find, or use LEN on column B again to count them.If any residue remains, add SUBSTITUTE handling for CHAR(160), and confirm the formula references the correct column.
Is the empty string being correctly recognized?Filter the placeholder column for "none" to check whether it matches the expected blank rowsCheck whether the condition uses LEN, rather than only writing A1="", since the two give different results for some data.
Are numbers and text being confused?Spot-check the LEN results in the number columnIf numbers are being treated as text, convert them all to a numeric format and then recalculate.

If the cleaning involves important content such as personal names, addresses, or contract terms, the results of machine processing still need to be checked by manual sampling; one cannot rely entirely on formula results.

📊 Core Comparison

Function or CombinationWhat can you do?What can't be done
TRIMRemove leading and trailing spaces and extra spaces in the middleDo not remove non-breaking spaces and control characters.
CLEANRemove invisible control charactersDoes not handle regular spaces or non-breaking spaces
SUBSTITUTEReplace specified characters, such as CHAR(160)The replacement target needs to be clearly specified; you cannot identify all anomalies at once.
LENCount the characters in a cell, and check for both blank cells and empty stringsRegardless of whether the character content is valid
COUNTIF "?*"Count text cells containing at least one characterIt isn't a counting figure, so it doesn't suit a total character count.
COUNTBLANKCount empty and blank-string cellsThe specific quantities of the two cannot be distinguished.

❓ Frequently Asked Questions (FAQ)

1. Why do COUNTIF blank counts and LEN checks give different results?

Usually the two differ in their counting criteria. COUNTIF(range,"") counts both truly empty cells and cells containing empty strings, while LEN(A1)=0 gives the same result when judged cell by cell. However, if a cell contains spaces, neither of them will count it. First use LEN to check the actual length of the cell, then decide on the counting criteria.

2. The non-breaking space is invisible, so how do you confirm it has been removed?

You can enter =CODE(MID(A1,n,1)) in a blank cell and check the character codes one by one; 160 is a non-breaking space. You can also use LEN on the columns before and after cleaning. If the values differ, the content was indeed removed.

3. The FILTER function doesn't work. How do I delete rows that contain empty strings?

You can use the filter feature to filter out 0 in the helper "length" column, then select the visible rows and delete them, and then clear the filter. This method does not update automatically; you need to repeat the operation after the data changes.

4. After cleaning, how do I turn empty strings into true empty cells?

The formula's "" still returns an empty string even after it is pasted as values. A more direct approach is to first filter for rows with length 0, then select those cells and clear their contents.

📝 Summary

To clear empty strings in Excel, the key is first to distinguish truly blank cells from cells containing empty strings, then use TRIM, CLEAN, and SUBSTITUTE to handle spaces and invisible characters. Character counting is done by combining LEN, SUMPRODUCT, and COUNTIF, and results are reliable only when totals are cross-checked against the original column before and after processing.

Open OmniDoc and process your spreadsheet file

Updated 2026-11-29