Alice has a column that contains text. In some cells there are duplicate words. For instance, a cell might contain "red red blue blue red." Alice would like to remove the duplicate words so the cell would contain "red blue" or "blue red." She wonders if it is possible to do that using a formula.
The answer is yes, this can be done using a formula, but only if you are using one of the latest versions of Excel. Consider the following, which will return the unique words from whatever text is in cell A1:
=TEXTJOIN(" ",,UNIQUE(TEXTSPLIT(TRIM(A1)," "),TRUE))
The formula splits the contents using TEXTSPLIT, then uses UNIQUE to remove duplicates. Finally, TEXTJOIN is used to put the words back into a single string. Note that because the initial splitting is done based on the location of spaces, if there are punctuation marks in the text, they are considered part of the word just before the mark. Thus, you could end up with "red blue. blue" because there is a period after one instance of "blue."
This formula requires at least Excel 2024 or Microsoft 365. If you are using an older version of Excel, or if you want to omit punctuation marks, then you would be best to use a macro. The following is a user-defined function that will get rid of the duplicates:
Function UniqueWords(InputString As String) As String
Dim AllWords() As String
Dim UniqueList() As String
Dim Nwords As Long
Dim i As Long
Dim j As Long
Dim sTemp As String
sTemp = LCase(InputString)
sTemp = Replace(sTemp, ".", "")
sTemp = Replace(sTemp, ",", "")
sTemp = Replace(sTemp, "!", "")
sTemp = Replace(sTemp, "?", "")
sTemp = Replace(sTemp, ":", "")
sTemp = Replace(sTemp, ";", "")
sTemp = Replace(sTemp, "(", "")
sTemp = Replace(sTemp, ")", "")
sTemp = Replace(sTemp, """", "")
sTemp = Trim(sTemp)
Do While InStr(sTemp, " ") > 0
sTemp = Replace(sTemp, " ", " ")
Loop
UniqueWords = ""
If Len(sTemp) = 0 Then Exit Function
AllWords = Split(sTemp)
Nwords = 0
ReDim UniqueList(0)
UniqueList(0) = " "
For i = LBound(AllWords) To UBound(AllWords)
For j = 0 To Nwords
If AllWords(i) = UniqueList(j) Then Exit For
Next j
If j > Nwords Then
' Word is new
ReDim Preserve UniqueList(Nwords + 1)
Nwords = Nwords + 1
UniqueList(Nwords) = AllWords(i)
If Nwords > 1 Then UniqueWords = UniqueWords & " "
UniqueWords = UniqueWords & AllWords(i)
End If
Next i
End Function
To use this UDF, simply enter the following in a cell:
=UniqueWords(A1)
The function removes punctuation using the multiple Replace statements. I purposely did not remove apostrophes because those could be used in contractions.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (13985) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, 2021, 2024, and Excel in Microsoft 365.
Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2019 For Dummies today!
How successful you are in copying information in Excel depends on lots of issues. This tip examines how those issues can ...
Discover MoreWhen you cut and paste rows using Ctrl+X and Ctrl+V, Excel leaves empty the rows where the cut information was previously ...
Discover MoreUnder the right circumstances, you may notice problems when copying dates from one workbook to another. This tip explains ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2026-07-15 09:38:35
J. Woolley
My most recent comment below discussed the following expression to remove punctuation characters from text in cell A1:
TEXTJOIN("", , TEXTSPLIT(A1, {"?",".","!"}))
Here's a better version of that expression with the same result:
CONCAT(TEXTSPLIT(A1, {"?",".","!"}))
I'm not sure Alice's column of text includes punctuation, and I doubt she wants to use a VBA UDF formula, but here's an alternative version of the Tip's UDF:
Function UniqueWords2(InputString As String) As String
Dim sTemp As String, item As Variant, UniqueItems As New Collection
Const PUNCTUATION = ". , ! ? : ; ( ) "" " 'two ("") become one (")
sTemp = InputString
For Each item In Split(WorksheetFunction.Trim(PUNCTUATION))
sTemp = Replace(sTemp, item, vbNullString) 'remove punctuation
Next item
On Error Resume Next
For Each item In Split(WorksheetFunction.Trim(sTemp))
UniqueItems.Add Null, item 'case insensitive
If Err = 0 Then 'item is unique
UniqueWords2 = UniqueWords2 & " " & item
Else
Err.Clear
End If
Next item
On Error GoTo 0
UniqueWords2 = LTrim(UniqueWords2) 'remove leading space
End Function
Notice UniqueItems only collects keys, where each key is a unique word from InputString. Collection keys are case insensitive, so this formula
=UniqueWords2(A1)
might return a mixed-case result. Here’s an alternative that converts all the text to lower-case:
=UniqueWords2(LOWER(A1))
If A1 contains "red, RED (BLUE) blue!" the first formula returns "red BLUE" and the second one returns "red blue".
Alice might want to amend the PUNCTUATION constant, which was derived from the Tip's version; if so, each punctuation character must be followed by a space character.
2026-07-14 15:32:25
J. Woolley
Re. TEXTSPLIT the Tip says, "... if there are punctuation marks in the text, they are considered part of the word just before the mark." For example, the Tip's first formula and the formulas in my previous comments below would reduce text like "red red? red blue! blue! blue" to "red red? blue! blue".
If we consider only question (?), period (.), and exclamation (!), the following expression will remove those punctuation characters from cell A1:
TEXTJOIN("", , TEXTSPLIT(A1, {"?",".","!"}))
So "red red? red blue! blue! blue" becomes "red red red blue blue blue". Therefore, using that expression instead of A1 in those formulas solves the punctuation issue noted in the Tip. The punctuation array {"?",".","!"} can be expanded if necessary.
2026-07-13 10:37:16
J. Woolley
Here’s a formula using a RegEx pattern suggested by Google Gemini to return text from cell A1 with duplicate words removed:
=TRIM(REGEXREPLACE(A1, "\b(\w+)\b(?=.*?\b\1\b)", "", 0, TRUE))
TRIM is necessary because the RegEx pattern results in extraneous space characters. The last argument (TRUE) is necessary to make REGEXREPLACE case insensitive; therefore, the result might be mixed-case. Here’s an alternative that converts all the text to lower-case:
=TRIM(REGEXREPLACE(LOWER(A1), "\b(\w+)\b(?=.*?\b\1\b)", "", 0))
The RegEx pattern in these formulas keeps the last instance of a word and removes earlier duplicates, so "red RED BLUE blue" becomes "blue RED" with the first formula and "blue red" with the second. I don’t believe Excel’s implementation of RegEx supports a pattern that keeps the first instance of a word and removes later duplicates.
REGEXREPLACE currently requires Excel 365, which also includes TRIMRANGE; therefore, the following formula returns an array with all duplicate words in column A reduced to single words (last instance first):
=TRIM(REGEXREPLACE(TRIMRANGE(A:A), "\b(\w+)\b(?=.*?\b\1\b)", "", 0, TRUE))
Here’s the alternative that returns lower-case to avoid a mixed-case result:
=TRIM(REGEXREPLACE(LOWER(TRIMRANGE(A:A)), "\b(\w+)\b(?=.*?\b\1\b)", "", 0))
And here's the equivalent using functions available in My Excel Toolbox:
=TRIM(ForEachCell(RangeTrim(A:A), "RegExSubstitute(LOWER(@), ""\b(\w+)\b(?=.*?\b\1\b)"", """", 0)"))
ForEachCell is discussed in my previous comment below. RangeTrim and RegExSubstitute work like TRIMRANGE and REGEXREPLACE.
See https://sites.google.com/view/MyExcelToolbox/
2026-07-11 11:41:47
J. Woolley
The Tip's first formula is
=TEXTJOIN(" ", , UNIQUE(TEXTSPLIT(TRIM(A1), " "), TRUE))
TRIM is used to remove extraneous space characters. But the default value of TEXTJOIN's second argument is TRUE, which tells it to ignore the single blank text value ("") received from UNIQUE that results when TEXTSPLIT is given text with extraneous space characters. Therefore, TRIM is unnecessary and the following formula is sufficient :
=TEXTJOIN(" ", , UNIQUE(TEXTSPLIT(A1, " "), TRUE))
These formulas keep the first instance of a word and remove later duplicates, so "red red blue blue red" becomes "red blue".
UNIQUE is case insensitive, so "red RED BLUE blue red" becomes "red BLUE". To remove mixed-case duplicates and return a lower-case result, use this formula:
=TEXTJOIN(" ", , UNIQUE(TEXTSPLIT(LOWER(A1), " "), TRUE))
TEXTSPLIT requires Excel 2024. Here's an alternative using My Excel Toolbox's SplitText function with Excel 2019's TEXTJOIN and Excel 2021's UNIQUE:
=TEXTJOIN(" ", , UNIQUE(SplitText(LOWER(A1), " "), TRUE))
Excel 2024 includes BYROW; this formula returns an array removing duplicate words from all cells in the range A1:A999
=BYROW(A1:A999, LAMBDA(cell, TEXTJOIN(" ", , UNIQUE(TEXTSPLIT(cell, " "), TRUE))))
If any cells are empty, BYROW returns #VALUE!, but this formula will fix that
=IFERROR(BYROW(A1:A999, LAMBDA(cell, TEXTJOIN(" ", , UNIQUE(TEXTSPLIT(cell, " "), TRUE)))), "")
Here's an alternative using My Excel Toolbox's ForEachCell and SplitText functions:
=IFERROR(ForEachCell(A1:A999,"TEXTJOIN("" "",,UNIQUE(SplitText(@,"" ""), TRUE))"), "")
ForEachCell is described in my comment dated 2025-01-20 here: https://excelribbon.tips.net/T011725
See https://sites.google.com/view/MyExcelToolbox/
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2026 Sharon Parq Associates, Inc.
Comments