|
こんにちは
「最後のN10、N20を残して」という事は順番も重要なのでしょうか?
「JOIN」だと順番が狂うようなので、文字連結で誤魔化すと、
Sub test()
Dim v As Variant
Dim A As String
Dim B As String
Dim i As Long
Dim m1Dic As Object
A = "N10,N20,N50,N10,N100,N10,N20"
v = Split(A, ",")
If IsEmpty(v) Then Exit Sub
Set m1Dic = CreateObject("Scripting.Dictionary")
For i = UBound(v) To LBound(v) Step -1
If Not IsEmpty(v(i)) Then
If Not m1Dic.Exists(v(i)) Then
B = v(i) & "," & B
m1Dic(v(i)) = v(i)
End If
End If
Next
MsgBox Left(B, Len(B) - 1)
Set m1Dic = Nothing
End Sub
「JOIN」版
Sub test1()
Dim v As Variant
Dim A As String
Dim B As String
Dim i As Long
Dim m1Dic As Object
A = "N10,N20,N50,N10,N100,N10,N20"
v = Split(A, ",")
Set m1Dic = CreateObject("Scripting.Dictionary")
For i = UBound(v) To LBound(v) Step -1
m1Dic(v(i)) = v(i)
Next
MsgBox Join(m1Dic.items, ",")
Set m1Dic = Nothing
End Sub
|
|