从一个单元格中的组合数据中删除重复项

时间:2018-06-18 07:03:08

标签: excel excel-formula

我希望删除一个单元格中的重复项,其中数据是从2个单元格组合而来的。例如:

单元格1:ABC& DEF
单元格2:ABC,DEF& LMN

我想要细胞1和细胞2要合并的2个数据,将同时删除重复数据并显示为:ABC,DEF&单元格3中的LMN。

感谢有人可以帮助我。提前谢谢。

***更正 - 对不起,如果我没有很好地输入细节。数据实际上是名称,例如:

Cell 1:John Mayer&布兰妮·斯皮尔斯 Cell 2:John Mayer,Britney Spears&赛琳娜戈麦斯

我想要的第3小组的结果:John Mayer,Britney Spears&赛琳娜戈麦斯

意思是单词之间有空格。逗号和符号也非常重要。*

3 个答案:

答案 0 :(得分:1)

也许这会有所帮助,直到公式出现。将其粘贴到模块中:

Public Function GetString(STR1 As String, STR2 As String) As String

Dim ARR1() As String, ARR2() As String, ARR3() As String
Dim X As Long, Y As Long, POS As Long

STR1 = Replace(STR1, " & ", ",")
STR1 = Replace(STR1, ", ", ",")
STR2 = Replace(STR2, " & ", ",")
STR2 = Replace(STR2, ", ", ",")

ARR1() = Split(STR1, ",")
ARR2() = Split(STR2, ",")

Y = 0
For X = LBound(ARR1) To UBound(ARR1)
    ReDim Preserve ARR3(Y)
    ARR3(Y) = ARR1(X)
    Y = Y + 1
Next X

For X = LBound(ARR2) To UBound(ARR2)
    ReDim Preserve ARR3(Y)
    ARR3(Y) = ARR2(X)
    Y = Y + 1
Next X

For X = LBound(ARR3) To UBound(ARR3)
    POS = InStr(GetString, ARR3(X))
    If POS <> 0 Then
        If Mid(GetString, POS + Len(ARR3(X)), 1) <> "," Then
            GetString = GetString & ARR3(X) & ", "
        End If
    Else
        GetString = GetString & ARR3(X) & ", "
    End If
Next X

GetString = Left(GetString, Len(GetString) - 2)
GetString = StrReverse(Replace(StrReverse(GetString), StrReverse(","), StrReverse(" &"), , 1))

End Function

称之为:

=GetString(Cell1, Cell2)

结果:

enter image description here

答案 1 :(得分:0)

试试这个公式:

 =A1&(SUBSTITUTE(A2,A1,""))

enter image description here

答案 2 :(得分:0)

可以通过公式实现。基本方法是:

  • 使用here中的标准拆分公式,但已修改,因此会生成一个字符串数组。

  • 将数组与自身匹配。如果第一个匹配位于当前元素之前,则它是一个重复的字符串,必须被忽略。

  • 使用textjoin加入其余元素。

  • 整理并找出一种用&amp;替换最终逗号的方法。登录。

首先将它放在一起用于包含(例如)ABC&amp;的单个细胞。 DEF

>> var data = "abc d abc gef a";
undefined
>> data
"abc d abc gef a"
>> var regex = /abc|gef/gi;
undefined
>> data.replace(regex,"fudge");
"fudge d fudge fudge a"

但是因为有两个单元格,分隔符可以是&#34;,&#34;或&#34;&amp;&#34;每个A1都会变成

=TEXTJOIN(",",true,IF(MATCH(
TRIM(MID(SUBSTITUTE(A1,"&",REPT(" ",LEN(A1))),(ROW(Z1:INDEX(Z:Z,LEN(A1)-LEN(SUBSTITUTE(A1,"&",""))+1))-1)*LEN(A1)+1,LEN(A1))),
TRIM(MID(SUBSTITUTE(A1,"&",REPT(" ",LEN(A1))),(ROW(Z1:INDEX(Z:Z,LEN(A1)-LEN(SUBSTITUTE(A1,"&",""))+1))-1)*LEN(A1)+1,LEN(A1))),
0)
<ROW(Z1:INDEX(Z:Z,LEN(A1)-LEN(SUBSTITUTE(A1,"&",""))+1)),"",
TRIM(MID(SUBSTITUTE(A1,"&",REPT(" ",LEN(A1))),(ROW(Z1:INDEX(Z:Z,LEN(A1)-LEN(SUBSTITUTE(A1,"&",""))+1))-1)*LEN(A1)+1,LEN(A1)))))

获得决赛#34;&#34;作为&#34;&amp;&#34;,有必要执行以下操作:

substitute(A1&","&A2,"&",",")

所以唯一明智的方法就是使用几个辅助细胞 -

在B1:

=substitute (*entire formula*,",","&",len(*entire formula*)-len(substitute(*entire formula*,",","")))

在C1:

=SUBSTITUTE(A1&" , "&A2,"&",",")

在D1中:

=TEXTJOIN(" , ",true,IF(MATCH(
TRIM(MID(SUBSTITUTE(B1,",",REPT(" ",LEN(B1))),(ROW($Z1:INDEX($Z:$Z,LEN(B1)-LEN(SUBSTITUTE(B1,",",""))+1))-1)*LEN(B1)+1,LEN(B1))),
TRIM(MID(SUBSTITUTE(B1,",",REPT(" ",LEN(B1))),(ROW($Z1:INDEX($Z:$Z,LEN(B1)-LEN(SUBSTITUTE(B1,",",""))+1))-1)*LEN(B1)+1,LEN(B1))),
0)
<ROW($Z1:INDEX($Z:$Z,LEN(B1)-LEN(SUBSTITUTE(B1,",",""))+1)),"",
TRIM(MID(SUBSTITUTE(B1,",",REPT(" ",LEN(B1))),(ROW($Z1:INDEX($Z:$Z,LEN(B1)-LEN(SUBSTITUTE(B1,",",""))+1))-1)*LEN(B1)+1,LEN(B1)))))

最后我必须在Google表格中测试它,因为我的Excel版本中没有textjoin

enter image description here

以下是Excel 2016中的外观&#p>

enter image description here

毋庸置疑,必须使用 Ctrl Shift 输入

将C1中的公式作为数组公式输入