如何删除用/分隔的单元格中的重复值?

时间:2013-04-04 07:36:31

标签: excel excel-vba excel-formula spreadsheet vba

我在excel中有多个单元格如下:

    b1= E4I8/E4I8/E4I8/E4I8
    b2=D3B2/B30C1/D3B2/D3B2/D3B2/B30C1


    multiple /xxxx/ 

如何在同一单元格中删除这些重复的文本字符串?

谢谢

2 个答案:

答案 0 :(得分:3)

此函数使用字典的键从传入的字符串构建唯一的部件列表(添加对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

您可以在Excel公式中使用此功能:

=UniqueParts("/","E4I8/E4I8/E4I8/E4I8")

或使用单元格引用:

=UniqueParts("/",B2)

您也可以在宏中迭代一系列单元格。

答案 1 :(得分:0)

如果允许使用VBA来解决您的问题,我建议使用以下功能:(无需引用!)

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

要在Excel中调用它,您需要输入两个参数:单元格和分隔标记:

=UniqueFromCell(B4,"/")