Waldir Leoncio
Waldir Leoncio

Reputation: 11411

Remove duplicates within Excel cell

Say I have the following text string in one single Excel cell:

John John John Mary Mary

I want to create a formula (so no menu functions or VBA, please) that would give me, on another cell

John Mary

How can I do this?

What I've tried so far was search the internet and SO about the issue and all I could find were solutions involving Excel's built-in duplicate removal or something involving countif and the replacement of duplicates for "". I've also taken a look at the list of Excel functions, especially those from the "Text" category, but couldn't find anything interesting, that could be done on one cell.

Upvotes: 9

Views: 57045

Answers (9)

JKVeganAbroad
JKVeganAbroad

Reputation: 332

With an Office 365 subscription, Excel provides additional formula functions, perfect for this exact task.

=TEXTJOIN(" ",TRUE,UNIQUE(TRANSPOSE(TEXTSPLIT(A1," "))))

This is identical to @user11308575 answer, except using Excel syntax rather than Google Docs syntax.

Personally, I used this with non-space delimiters (","), but to answer the question correctly, the formula was written this way.

Upvotes: 0

JvdV
JvdV

Reputation: 75990

If one has access to TEXTJOIN one could use:

=TEXTJOIN(" ",,FILTERXML("<t><s>"&SUBSTITUTE(A1," ","</s><s>")&"</s></t>","//s[not(preceding::*=.)]"))

Upvotes: 0

Arne Balks
Arne Balks

Reputation: 1

Did you try the textjoin function? (available in Excel 2016, not sure about previous versions). Was just looking for something similar and this seems to do the job for me on a column where I have multiple values more than once.

=TEXTJOIN(delimiter;ignore_empty;text)
  • define delimiter in any way you need it
  • ignore empty can be true or false, depending on what serves your needs
  • text would be your array of values - using the unique function within here (see example below) will filter out any multiples of any string (I am using it for numbers and it works)

Example:

=TEXTJOIN(" ";TRUE;UNIQUE($A$1:$A$16))

Guess this might be Excel's equivalent to google sheets' join function. Textjoin comes up if you type in =join - I took the formula provided in user11308575's post above but removed the parantheses and its content, then went from there.

Hope this helps (even though the thread is already old) ;)

Upvotes: 0

Madhav Kulkarni
Madhav Kulkarni

Reputation: 69

The answer is here: https://www.extendoffice.com/documents/excel/2133-excel-remove-duplicate-characters-in-string.html

Function RemoveDupes2(txt As String, Optional delim As String = " ") As String
Dim x
'Updateby20140924
With CreateObject("Scripting.Dictionary")
    .CompareMode = vbTextCompare
    For Each x In Split(txt, delim)
        If Trim(x) <> "" And Not .exists(Trim(x)) Then .Add Trim(x), Nothing
    Next
    If .Count > 0 Then RemoveDupes2 = Join(.keys, delim)
End With
End Function

Put the code above in a module

Use =RemoveDupes2(A2,",") A2 contains repeated text separated by , You may change the delimiter

Upvotes: 6

user11308575
user11308575

Reputation: 1

I found the answer below in this thread https://superuser.com/questions/643909/remove-duplicate-entries-in-one-cell

=join(" ",unique(transpose(split(A1," "))))

Upvotes: -1

user10291834
user10291834

Reputation: 1

Found a solution that might work if you are also the one making the list.

when you make the list if you are doing it by combining the cell above with the current line, you can check to see if the value is already in the above cell using the following code:

if(iserror(find(value_to_be_added,previous_concatenation)),
    previous_concatenation&" "&value_to_be_added,previous_concatenation)

Upvotes: 0

Rick Hitchcock
Rick Hitchcock

Reputation: 35680

Assuming you'll never have more than two distinct names in a cell, this should work:

=MID(A1&" ",1,FIND(" ",A1&" "))&
 MID(SUBSTITUTE(A1&" ",MID(A1&" ",1,FIND(" ",A1&" ")),"")&" ",1,
 FIND(" ",SUBSTITUTE(A1&" ",MID(A1&" ",1,FIND(" ",A1&" "))&" ","")))

It will show John Mary for all of these:

John John John Mary Mary
John Mary
John Mary John Mary
John Mary Mary
John John Mary

It will show John for all of these:

John
John John
John John John

And it will show nothing if A1 is blank.

Upvotes: 1

pnuts
pnuts

Reputation: 59495

Without a formula: Text to Columns with space as the delimiter, transpose the output, apply Remove Duplicates to each of the columns individually, then transpose the result.

Upvotes: 0

Ron Rosenfeld
Ron Rosenfeld

Reputation: 60474

As I wrote, it is trivial to solve with VBA. If you cannot use VBA, one method is to use helper columns.

Assume: Your string is in A1

Enter the following formulas:

C1:  =IFERROR(INDEX(TRIM(MID(SUBSTITUTE($A$1," ",REPT(" ",99)),(ROW(INDIRECT("1:" & LEN($A$1)-LEN(SUBSTITUTE($A$1," ",""))+1))-1)*99+((ROW(INDIRECT("1:" & LEN($A$1)-LEN(SUBSTITUTE($A$1," ",""))+1))=1)),99)),ROWS($1:1),1),"")

D1:  =IF(COUNTIF(C1:$C$5,C1)=1,C1,"")

Select C1 and D1 and fill down until you start getting blanks

E1:  =D1
E2:  =TRIM(CONCATENATE(D2," ",E1))

Select E2 and fill down.

The contents of the last cell filled in column E will be your result.

If you want to have a cell which automatically returns the contents of the last cell in column E range, you can use a formula like:

=LOOKUP(REPT("z",99),$E$1:$E$100)

Upvotes: 0

Related Questions