Remove duplicates within Excel cell

痴心易碎 提交于 2019-12-19 03:33:50

问题


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.


回答1:


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 =remove(A2,",") A2 contains repeated text separated by , You may change the delimiter




回答2:


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)



回答3:


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.




回答4:


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.




回答5:


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)



回答6:


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

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



来源:https://stackoverflow.com/questions/25897429/remove-duplicates-within-excel-cell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!