Removing Duplicate values from a string in VBA

前端 未结 5 2056
伪装坚强ぢ
伪装坚强ぢ 2020-12-18 17:05

In VBA if I have a string of numbers lets say ("1,2,3,4,5,2,2"), how can I remove the duplicate values and only leave the first instance so the string says ("

5条回答
  •  既然无缘
    2020-12-18 17:11

    try this:

    Sub test()
        Dim S$: S = "1,2,3,4,5,2,2,5,6,6,6"
        Dim Dic As Object: Set Dic = CreateObject("Scripting.Dictionary")
        Dim Key As Variant
        For Each Key In Split(S, ",")
            If Not Dic.exists(Trim(Key)) Then Dic.Add Trim(Key), Nothing
        Next Key
        S = Join(Dic.Keys, ","): MsgBox S
    End Sub
    

提交回复
热议问题