问题
I have multiple cells in excel that have as follows:
b1= E4I8/E4I8/E4I8/E4I8
b2=D3B2/B30C1/D3B2/D3B2/D3B2/B30C1
multiple /xxxx/
How do I remove these duplicate text strings in the same cell?
Thank you
回答1:
This function uses the keys of a dictionary to build a unique list of parts from a passed-in string (add a reference to Microsoft Scripting Runtime):
Public Function UniqueParts(separator As String, toParse As String) As String
Dim d As New Scripting.Dictionary, part As Variant, i As Integer
For Each part In Split(toParse, separator)
d(part) = 1
Next
UniqueParts = Join(d.Keys, "/")
End Function
You can use this function in an Excel formula:
=UniqueParts("/","E4I8/E4I8/E4I8/E4I8")
or with a cell reference:
=UniqueParts("/",B2)
You can also use this inside a macro that iterates over a range of cells.
回答2:
If using VBA is allowed to solve you problem I propose the following function: (no references required!)
Function UniqueFromCell(rngCell, splitString)
Dim myCol As New Collection
Dim itmCol
Dim i As Long
Dim arrTMP As Variant
arrTMP = Split(rngCell, splitString)
For i = 1 To UBound(arrTMP)
On Error Resume Next
myCol.Add arrTMP(i), CStr(arrTMP(i))
On Error GoTo 0
Next i
Dim result
For Each itmCol In myCol
result = result & itmCol & splitString
Next
UniqueFromCell = Left(result, Len(result) - Len(splitString))
End Function
To call it in Excel you need to put two parameters: cell and separation mark:
=UniqueFromCell(B4,"/")
来源:https://stackoverflow.com/questions/15805157/how-do-i-remove-duplicate-values-in-a-cell-seperated-with-a