VBA – Deleting duplicated in one string which are separated by semicolon

Please update your question to show what you have already tried. It is very possible that your current code only needs a bit of tweaking.

Below is a more general way to accomplish what you’re after, but like I said, there may be an easier way to fix your current code and you won’t really learn by copying and pasting code.

Function RemoveDuplicates(rng as Range) As String
    Dim dict As Object
    Dim var As Variant, v As Variant

    Set dict = CreateObject("Scripting.Dictionary")

    var = Split(rng.Value,";")

    For each v in var
        If Not dict.Exists(v) Then
            dict.Add v, v
        End If
    Next v

    RemoveDuplicates = Join(dict.Keys, ";")
End Function

Leave a Comment