Excel formula to remove duplicates in a cell

sharshra

Active Member
Joined
Mar 20, 2013
Messages
276
Office Version
  1. 365
I have an output from TEXTJOIN, which has duplicates. I need unique values as the final output. I searched internet & found there are VBA UDFs to remove duplicates in a cell. But I want to achieve the same using Excel formulae.

Are there any options to remove duplicates in a cell using Excel formulae? Or is it possible to have an output from TEXTJOIN with unique values only? Please advise.

Example for illustration only-
Output from TEXTJOIN - abc, def, abc, xyz, bcd, cde, def, mno
Desired output without duplicates - abc, def, xyz, bcd, cde, mno
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Let's say your list is in the range A1:A10. Try the formula:

Excel Formula:
=TEXTJOIN(",",TRUE,UNIQUE($A$1:$A$10))
 
Upvote 0
As mentioned in my post, I'm already using TEXTJOIN to get an output. Question is about removing duplicates in a single cell, NOT on range of cells.
 
Upvote 0
Here's a UDF you can use until a worksheet functions formula is offered.
Note in the example below that ", " is used as the delimiter not just ",".
Book1
AB
1abc, def, abc, xyz, bcd, cde, def, mnoabc, def, xyz, bcd, cde, mno
2123, 456, 789123, 456, 789
3abc, def, xyzabc, def, xyz
4 
Sheet1
Cell Formulas
RangeFormula
B1:B4B1=IF(A1="","",UniquesOnly(A1, ", "))

VBA Code:
Function UniquesOnly(S As String, Delim As String) As String
Dim V As Variant, d As Object, i As Long, X As Variant
V = Split(S, Delim)
Set d = CreateObject("Scripting.dictionary")
For i = LBound(V) To UBound(V)
    If Not d.exists(V(i)) Then
        d.Add V(i), d.Count + 1
        X = X & Delim & V(i)
    End If
Next i
UniquesOnly = Mid(X, Len(Delim) + 1, Len(X) - Len(Delim) + 1)
End Function
 
Upvote 0
Thanks, Joe. I'm already using UDF, but wanted to achieve similar result using formulae.
 
Upvote 0
Assuming the text in your cell is never longer than 300 characters (make all three 300s larger by the same amount if it could be), then this formula seems to work...
Excel Formula:
=TEXTJOIN(", ",,UNIQUE(TRIM(MID(SUBSTITUTE(","&A1,",",REPT(" ",300)),SEQUENCE(1+LEN(A1)-LEN(SUBSTITUTE(A1,",","")))*300,300))))
 
Upvote 0
Solution
Thanks, Rick. That works perfectly (y)

Though I couldn't understand how it works, this is doing what I expected. Now trying to decipher it :giggle: Brief explanation will be of great help :)
 
Upvote 0
Are there any options to remove duplicates in a cell using Excel formulae?
Another option would be:

21 12 09.xlsm
AB
1abc, def, abc, xyz, bcd, cde, def, mnoabc, def, xyz, bcd, cde, mno
No Dupes
Cell Formulas
RangeFormula
B1B1=TEXTJOIN(", ",,UNIQUE(FILTERXML("<p><c>"&SUBSTITUTE(A1,", ","</c><c>")&"</c></p>","//c")))



Or is it possible to have an output from TEXTJOIN with unique values only?
If you shared the original TEXTJOIN formula and perhaps some sample data to see layout, that could be investigated.
 
Upvote 0
Thanks, Peter. As always, you have the solution :)

As in the case of Rick's solution, I couldn't figure out how the formula works. Scratching my head to understand it better. Any further insights on how it works will be greatly appreciated.

I'm happy that now there are 2 solutions (y) Both works for me, but I can mark only one as solution :giggle:
 
Upvote 0
Any further insights on how it works will be greatly appreciated.
Not simple to explain the detail of the FILTERXML function (perhaps search the forum for more examples with FILTERXML) but it splits the delimited string into a list of individual values then UNIQUE removes the duplicates and TEXTJOIN puts what is left back into a delimited string.

In post 1 you seemed to also be asking if your original TEXTJOIN function could get the unique values directly but you didn't address that part of my last post. :)
 
Upvote 0

Forum statistics

Threads
1,214,987
Messages
6,122,614
Members
449,090
Latest member
vivek chauhan

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top