Excel Duplicate Character Replacement Guide: Remove Consecutive, All, and Word Duplicates

How to Select and Replace Duplicate Characters and Words in Excel: Clean Up Consecutive and All Duplicates at Once

Replacing duplicate characters in Excel can be divided into two types. First, compress consecutive duplicates into one character (for example, aaabbcc → abc). Second, remove duplicates across the entire text, keeping only the first occurrence (for example, abcaad → abcd). This guide also covers word-level duplicates with TEXTSPLIT, selecting duplicate values in a range and using Replace (Find and Replace), and bulk processing with Power Query.

Quick Fix

① Compress consecutive duplicate characters (aaabb → ab)

Microsoft 365/2021 or later

=LET(
  s, A2,
  ch, MID(s, SEQUENCE(LEN(s)), 1),
  prev, VSTACK("", DROP(ch, -1)),
  keep, ch<>prev,
  TEXTJOIN("",, FILTER(ch, keep))
)

This removes characters that match the immediately preceding character, leaving only one.

② Remove all duplicate characters (keep first occurrence, preserve order)

Microsoft 365/2021 or later

=LET(
  s, A2,
  ch, MID(s, SEQUENCE(LEN(s)), 1),
  pos, SEQUENCE(LEN(s)),
  first, XMATCH(ch, ch, 0),
  TEXTJOIN("",, FILTER(ch, first=pos))
)

This removes all duplicates by keeping a character only when its first position matches its current position.

③ Clean up duplicate words

  • Compress consecutive duplicate words (for example, red red blue blue → red blue):
    =LET(arr,TEXTSPLIT(A2," "),TEXTJOIN(" ",,FILTER(arr, arr<>VSTACK("",DROP(arr,-1)))))
  • Remove all duplicate words (keep only the first occurrence):
    =LET(arr,TEXTSPLIT(A2," "),pos,SEQUENCE(ROWS(arr)),first,XMATCH(arr,arr,0),TEXTJOIN(" ",,FILTER(arr, first=pos)))
  • For other delimiters (such as commas and semicolons), use an array delimiter such as TEXTSPLIT(A2,{",",";"}).

④ Select duplicates in a range, then use Replace (Find and Replace)

  1. Conditional Formatting → Highlight Cells Rules → Duplicate Values → choose a color.
  2. Turn on the filter button (AutoFilter), then use Filter by Color → display duplicates only.
  3. Use Ctrl+H (Find and Replace) to replace the desired value or string in bulk.
    Example: Replace a “duplicate” marker with “(dup)”.
  4. If you want to delete entire rows, use Data → Remove Duplicates (row-based).

Why This Happens: Concepts and Principles

  • Consecutive duplicates are a question of whether a character matches the one immediately before it. Split the text into individual characters and compare each one with the previous character.
  • All duplicates are a question of whether a character is its first occurrence. Use XMATCH to find the first position of each character, then keep only those whose first position matches the current position.
  • Duplicate words are handled with the same logic after splitting a sentence into a word array with TEXTSPLIT.
  • Duplicates in a range are duplicate cell values (row-based), which is different from duplicate text within a cell.

Practical Examples

Sample Data

RowA (Original)Goal
2aaabbccddeCompress consecutive duplicates → abcde
3abcaadRemove all duplicates (first occurrences only) → abcd
4red red blue blue redClean up duplicate words
5010-1234-5678Clean up duplicate hyphens (consecutive symbols)
6apple;apple;pear;pear;appleDuplicate words separated by semicolons

1) How the consecutive duplicate character formula works

The key is to keep a character only when the “current character ≠ previous character.”

=LET(
  s, A2,
  ch, MID(s, SEQUENCE(LEN(s)), 1),          /* character array */
  prev, VSTACK("", DROP(ch, -1)),            /* match the length using the character one position earlier */
  keep, ch<>prev,                            /* keep when different */
  TEXTJOIN("",, FILTER(ch, keep))            /* combine the kept characters */
)

2) How the formula to remove all duplicate characters works

Keep a character only when its position in the array, 1..n, matches its first position.

=LET(
  s, A3,
  ch, MID(s, SEQUENCE(LEN(s)), 1),
  pos, SEQUENCE(LEN(s)),
  first, XMATCH(ch, ch, 0),
  TEXTJOIN("",, FILTER(ch, first=pos))
)

3) Clean up duplicate words

  • Compress consecutive duplicate words:
    =LET(arr,TEXTSPLIT(A4," "),
    TEXTJOIN(" ",,FILTER(arr, arr<>VSTACK("",DROP(arr,-1)))))
    
  • Remove all duplicate words (first occurrences only):
    =LET(arr,TEXTSPLIT(A6,";"),
    pos,SEQUENCE(ROWS(arr)),
    first,XMATCH(arr,arr,0),
    TEXTJOIN(";",,FILTER(arr, first=pos)))
    

4) Clean up consecutive duplicate symbols (hyphens/commas)

To compress multiple identical symbols into one, split the text into characters and use the same method, or use a simple replacement.

  • Replacement example (two or more hyphens → one): Repeatedly apply =SUBSTITUTE(SUBSTITUTE(A5,"--","-"),"---","-"), or generalize it with the consecutive duplicate compression pattern above for a more reliable solution.

5) Structured reference tables and dynamic array expansion

If you convert the range to a table (Ctrl+T), name it tbl, and use [Raw] as the column name, apply consecutive duplicate compression to the entire column:

=LET(s, tbl[Raw], ch, MID(s,SEQUENCE(LEN(s)),1),
prev, VSTACK("", DROP(ch,-1)), keep, ch<>prev,
TEXTJOIN("",,FILTER(ch, keep)))

Because of spilling, place the results column in a regular range, or use Power Query for a permanent transformation.

6) Bulk processing with Power Query

  1. Data > From Table/Range → open Power Query.
  2. Split Column → By Delimiter (space, semicolon, etc.) → Split into Rows.
  3. Remove duplicate keys (keep only the first occurrence). To compress only consecutive duplicates, add an index, compare with the previous row, and filter.
  4. Group By → All Rows → recombine using Text.Combine.
  5. Use Close & Load to apply the results to the worksheet (refreshable and preserves the source data).

Alternative Methods, Notes, and Checklist

  • Distinguish between consecutive duplicates and all duplicates. They serve different purposes.
  • If the data contains invisible spaces (CHAR(160)), tabs, or line breaks, clean it first with TRIM/CLEAN/SUBSTITUTE.
  • For deleting duplicate rows in a range, Data → Remove Duplicates is the fastest option. This is separate from duplicate text within a cell.
  • If you need regular expressions, consider Power Query or Office Script/VBA.
  • For repeated tasks, wrap the formula in a LAMBDA custom function to improve reusability.

Troubleshooting

IssueCauseSolution
The formula is too slow Repeated character splitting and filtering in large datasets Use Power Query, or split the process into stages with LAMBDA and helper columns
Word boundaries are inconsistent Mixed delimiters (spaces, commas, semicolons) Use multiple delimiters, such as TEXTSPLIT(text,{",",";"," "})
Korean text is corrupted Byte functions (such as MIDB) are used together Use only Unicode-based MID/LEFT/RIGHT
Replace changes only some values The filter or selected range is limited Use Conditional Formatting to mark values by color → filter by color → select all → press Ctrl+H

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *