简体   繁体   English

删除 Excel 单元格中的重复项

[英]Remove duplicates within Excel cell

Say I have the following text string in one single Excel cell:假设我在一个 Excel 单元格中有以下文本字符串:

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我想在另一个单元格上创建一个公式(所以没有菜单功能或 VBA,请)

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 "" .到目前为止,我所尝试的是在互联网上搜索有关该问题的内容,我所能找到的只是涉及 Excel 的内置重复删除或涉及countif和替换""重复项的解决方案。 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.我还查看了 Excel 函数列表,尤其是“文本”类别中的函数列表,但找不到任何可以在一个单元格上完成的有趣内容。

The answer is here: https://www.extendoffice.com/documents/excel/2133-excel-remove-duplicate-characters-in-string.html答案在这里: 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使用=RemoveDupes2(A2,",") A2 包含由 分隔的重复文本,您可以更改分隔符

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 Mary所有的这些:

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 John

And it will show nothing if A1 is blank.如果A1为空,它将不显示任何内容。

As I wrote, it is trivial to solve with VBA.正如我所写的,用 VBA 解决是微不足道的。 If you cannot use VBA, one method is to use helper columns.如果您不能使用 VBA,一种方法是使用辅助列。

Assume: Your string is in A1假设:您的字符串在 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选择 C1 和 D1 并向下填充直到开始出现空白

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

Select E2 and fill down.选择 E2 并向下填充。

The contents of the last cell filled in column E will be your result.在 E 列中填写的最后一个单元格的内容将是您的结果。

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:如果您想要一个自动返回 E 列范围内最后一个单元格内容的单元格,您可以使用如下公式:

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

没有公式:文本到以空格为分隔符的列,转置输出,分别对每一列应用删除重复项,然后转置结果。

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)

Did you try the textjoin function?您是否尝试过 textjoin 功能? (available in Excel 2016, not sure about previous versions). (在 Excel 2016 中可用,不确定以前的版本)。 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) text 将是您的值数组 - 使用此处的唯一函数(参见下面的示例)将过滤掉任何字符串的任何倍数(我将它用于数字并且它有效)

Example:例子:

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

Guess this might be Excel's equivalent to google sheets' join function.猜猜这可能是 Excel 等价于 google sheet 的 join 功能。 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.如果您输入 =join,就会出现 Textjoin - 我采用了上面 user11308575 的帖子中提供的公式,但删除了括号及其内容,然后从那里开始。

Hope this helps (even though the thread is already old) ;)希望这会有所帮助(即使该线程已经很旧);)

如果可以访问TEXTJOIN则可以使用:

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

我在此线程中找到了以下答案https://superuser.com/questions/643909/remove-duplicate-entries-in-one-cell

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM