Sub topic 17
Cleaning and Reshaping Entered Data
This chapter assumes you can enter values, recognize text versus numbers versus dates, use copy and paste special, and use find and replace. It builds on those skills to fix data that arrives messy: pasted from a website, exported from another system, or typed inconsistently by several people.
Why imported data needs cleaning
Data from outside Sheets often carries problems that are invisible at a glance:
- Extra spaces at the start or end of a value, or doubled spaces inside it.
- Several values packed into one cell, such as
Smith, Jane, Sales. - Numbers stored as text, so they refuse to add up.
- Non-printing characters, such as the non-breaking space that web pages use.
- Duplicate rows created by repeated exports or merged lists.
These problems cause wrong results quietly. A lookup fails because "Apple " is not equal to "Apple". A sum skips a value because it is text. Cleaning means finding these problems and fixing them in a way you can repeat and check.
A safe workflow
Most cleaning steps overwrite data, so work in a way that lets you recover.
- Keep the original. Duplicate the sheet (right-click the tab, then Duplicate) or leave the raw data in its own sheet and clean a copy.
- Clean with formulas in helper columns first. The original stays untouched, and you can compare before and after.
- Convert formulas to values only when you are satisfied. Copy the helper column, then use Paste special → Values only over the target.
- Delete the helper columns once their results have been pasted as values.
Removing extra spaces with TRIM
TRIM removes leading and trailing spaces and collapses repeated spaces inside the text to a single space.
=TRIM(A2)If A2 holds " North region ", the result is "North region".
Fill the formula down a helper column next to the data. You can also use the built-in tool: select the range, then choose Data → Data cleanup → Trim whitespace. The menu command changes the cells in place, so it is best used on a copy or when you are sure.
When TRIM seems to do nothing
Text copied from web pages often contains a non-breaking space, character code 160. TRIM removes only the ordinary space, character code 32. To check, compare lengths:
=LEN(A2)If the length is larger than the visible characters suggest, a hidden character is present. Replace it first, then trim:
=TRIM(SUBSTITUTE(A2,CHAR(160)," "))For other non-printing characters, CLEAN removes the ASCII control characters:
=TRIM(CLEAN(A2))Splitting one cell into several
Two methods split text across columns.
The SPLIT function
=SPLIT(A2,",")SPLIT places each comma-separated piece of A2 into its own cell across the row, starting at the formula cell. The cells to the right must be empty. If they are not, Sheets shows a #REF! error and says the array result was not expanded.
Points to know:
- By default, each character in the delimiter argument is treated as a separate delimiter.
SPLIT(A2,", ")splits on commas and on spaces. To treat the delimiter as one exact string, passFALSEas the third argument:=SPLIT(A2,", ",FALSE). - Consecutive delimiters are collapsed by default, so empty pieces are dropped. Pass
FALSEas the fourth argument to keep empty cells. - Pieces that look like numbers or dates are converted to those types, as in normal entry. A product code such as
00123becomes123.
The menu command
Select the column, then choose Data → Split text to columns. A small Separator control appears. Pick comma, semicolon, period, space, or a custom character. This overwrites cells in place and also removes the need for empty cells to the right only if you accept overwriting them, so check what lies to the right first.
| Method | Updates when source changes | Overwrites cells | Best for |
|---|---|---|---|
| `SPLIT` formula | Yes | No, but needs empty cells to the right | Ongoing imports, checking results |
| Split text to columns | No | Yes | One-time cleanup |
Fixing numbers and dates stored as text
A number stored as text is usually left-aligned and is ignored by SUM. Common causes are an apostrophe prefix, a thousands separator in the wrong locale, or a stray space.
VALUE converts text to a number:
=VALUE(TRIM(A2))A quick alternative is to multiply by 1, such as =A2*1. Either approach returns #VALUE! if the text cannot be read as a number, which helps you find the bad entries. If the conversion fails on values like 1.234,50, the cause is usually a locale mismatch, covered in the locale chapter.
To turn a date stored as text into a real date, use DATEVALUE, then format the result as a date:
=DATEVALUE(A2)To go the other way and produce text from a real date, use TEXT:
=TEXT(A2,"yyyy-mm-dd")The result is text, so it will not sort or calculate as a date.
Removing duplicates
Select the range, then choose Data → Data cleanup → Remove duplicates. The dialog lets you choose:
- Whether the data has a header row.
- Which columns to compare. Two rows count as duplicates only if they match in every selected column.
Sheets keeps the first occurrence and deletes later ones, then reports how many rows were removed. Comparison ignores nothing by default, so "Apple" and "apple " are different values to the tool. Trim and normalize case before removing duplicates.
To normalize case, use LOWER, UPPER, or PROPER:
=PROPER(TRIM(A2))If you want to review duplicates before deleting them, a formula in a helper column can flag them. This version marks every repeat after the first:
=COUNTIF($A$2:A2,A2)>1The range starts fixed at $A$2 and grows as the formula fills down, so each row counts only the matches at or above it.
Another option is to produce a de-duplicated list without touching the original:
=UNIQUE(A2:C100)Other cleanup tools
The Data cleanup menu also offers Cleanup suggestions, which scans a column for inconsistent formats and near-duplicate values and proposes fixes. It is useful for a first pass, but read each suggestion before accepting it. Find and replace with regular expressions handles patterns such as removing a unit suffix from every value.
Workflow in one view
Common errors
#REF!from SPLIT. Cells to the right are not empty. Clear them or move the formula.- Leading zeros disappear. The value was read as a number. Format the column as Plain text before pasting, or use a text format code.
- Remove duplicates deletes too much. Too few columns were selected for comparison. Select every column that defines a unique row.
- TRIM leaves a visible gap. A non-breaking space remains. Use
SUBSTITUTEwithCHAR(160). - Helper formulas turn into errors. The source was deleted before the results were pasted as values.
Exercise
Cell A2 contains " Acme Corp ", and a list of names copied from a website still fails to match after you apply =TRIM(A2). Write a formula that fixes both the spacing and a possible non-breaking space, then name the safe way to replace the original column with the result.
Show answer
Use =TRIM(SUBSTITUTE(A2,CHAR(160)," ")). SUBSTITUTE turns any non-breaking space (character 160) into an ordinary space, and TRIM then removes leading, trailing, and repeated spaces. Fill the formula down a helper column, copy the helper column, and use Paste special → Values only over the original column. Delete the helper column afterward.
Exercise
A2 contains red,,blue. What does =SPLIT(A2,",") return, and how do you keep the empty piece?
Show answer
It returns two cells, red and blue, because consecutive delimiters are collapsed by default. To keep the empty cell, pass FALSE as the fourth argument: =SPLIT(A2,",",TRUE,FALSE). The third argument, which controls splitting by each character, is TRUE by default.