excel - Remove duplicates from combined data in one cell
问题描述
I'm looking to remove duplicates in one cell where the data is combined from 2 cells. For example :
cell 1 : ABC & DEF
cell 2 : ABC, DEF & LMN
I want both cells 1 & 2 data to combine, which will remove the duplicates at the same time and appear to be : ABC, DEF & LMN in cell 3.
Appreciate if someone can help me on this. Thank you in advance.
***Correction - Sorry if I didn't input the details well. The data are actually names, for example :
Cell 1 : John Mayer & Britney Spears Cell 2 : John Mayer, Britney Spears & Selena Gomez
The outcome I want in Cell 3 : John Mayer, Britney Spears & Selena Gomez
Meaning that there are spaces in between the words. The commas and ampersand is very important as well.*
解决方案
在公式出现之前,这可能会有所帮助。将其复制粘贴到模块中:
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)
结果:
推荐阅读
- android - 如何获取屏幕上未显示的 RecyclerView 项目的数据?
- java - 基于套接字的应用程序的非确定性行为
- psql - Google Cloud SQL Postgres(Set Session_Replication_Role='Replica') || 布卡多
- javascript - 确认 Google 助理的异步操作
- python - Python - 将列表变成卷轴
- azure-functions - Azure 代理超时
- java - javax.persistence.PersistenceException:EntityManager 没有持久性提供程序
- javascript - 如何离线保存使用 ember-cli-fastboot 呈现的网页?
- python - 多数的幂
- java - 将 JAXB 元素编入 SOAP 处理转义字符