Sorting string from an array
Sorting string from an array
(OP)
I have a list of strings and I would like collect from that list only one time if the strings the same for example if an array("A", "B", "C", "A", "B", "B")
and I would like to collect ("A", "B", "C"). How do I do that. Thanks in advance
and I would like to collect ("A", "B", "C"). How do I do that. Thanks in advance





RE: Sorting string from an array
Regards,
Regg
RE: Sorting string from an array
I am kind of new to VB, I tried to collect like you suggested, can you give me sample code.
Thanks
Sub Sorting()
Dim ListString
Dim CollList As New Collection
ListString = Array("A", "B", "B", "A", "C")
i = 0
Do
CollList.Add ListString(i)
If i = UBound(ListString) Then Exit Do
i = i + 1
Loop
Debug.Print CollList.count
End Sub
RE: Sorting string from an array
Sub GetUniqueEntries()
'note add a reference to Microsoft Scripting runtime to be able to use dictionary objects
Dim ListString, i As Integer
Dim DicList As New Dictionary
ListString = Array("A", "B", "B", "A", "C")
For i = 0 To UBound(ListString)
If DicList.Exists(ListString(i)) Then
Else
DicList.Add ListString(i), ""
End If
i = i + 1
Next
For i = 0 To DicList.Count - 1
Debug.Print DicList.Keys(i)
Next
End Sub
RE: Sorting string from an array
Sorry for taking so long to answer your reply.
What I meant was that collections do not allow duplicate keys. Try this code:
CODE
'declare variables
Dim ListString As Variant
Dim Item As Variant
'create collection
Dim CollList As New Collection
'create test array
ListString = Array("A", "B", "B", "A", "C")
'set error routine to skip duplicate key
On Error Resume Next
'loop through test array
For Each Item In ListString
'add item to collect using item as both item and key
'collections do not allow duplicate keys and raises error 457
'On Error Resume Next lets program continue on with stopping
CollList.Add Item, Item
Next
'turn off error handling
On Error GoTo 0
'display number of collection items in immediate window
Debug.Print CollList.Count
End Sub
Regards,
Regg
RE: Sorting string from an array
RE: Sorting string from an array
Just out of curiosity, how did you solve your problem?
Regards,
Regg