In this demo I take a comma seperated list that is out of order and contains duplicates ... the resulting comma seperated list is in order and contains no duplicates...Is that what you looking for ?
Option Compare Text
Public Sub DemoHere()
Def = "F,d,a,rod,k,BCA,d,ABC,a,d,I,b,nim,c,K"
TestList = Application.InputBox("Please Enter Scrambled List", "Demo", Def)
If TestList = False Then Exit Sub
ResultList = SortToUniqueList(TestList)
ln1 = "Orginal List : " & TestList & vbNewLine
ln2 = "Result List: " & ResultList
pt = MsgBox(ln1 & ln2, vbInformation, "Resulting List")
End Sub
Private Function SortToUniqueList(TestList) As String
Dim listCollection As New Collection
Dim CollectionMember
'MAKE UNIQUE COLLECTION
On Error Resume Next
For Each ValStr In Split(TestList, ",")
listCollection.Add ValStr, CStr(ValStr)
Next ValStr
'turn off error handling
On Error GoTo 0
' SORT COLLECTION
For Sort1 = 1 To listCollection.Count - 1
For Sort2 = Sort1 + 1 To listCollection.Count
If listCollection(Sort1) > listCollection(Sort2) Then
Tmp1 = listCollection(Sort1)
Tmp2 = listCollection(Sort2)
'Swap the items over
listCollection.Add Tmp1, , Sort2
listCollection.Add Tmp2, , Sort1
'Delete the original items
listCollection.Remove Sort1 + 1
listCollection.Remove Sort2 + 1
End If
Next
Next
'make comma sep string list
For Each CollectionMember In listCollection
If Len(Trim(CollectionMember)) > 0 Then
If FinalList = "" Then
FinalList = CollectionMember
Else
FinalList = FinalList & "," & CollectionMember
End If
End If
Next
SortToUniqueList = FinalList
End Function
Bookmarks