×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Sorting string from an array

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

RE: Sorting string from an array

One way would be to put the array into a collection, since you cannot have duplicate data in a collection, only those strings not equal would be in the collection.

Regards,

Regg

RE: Sorting string from an array

(OP)
Hi Regg,
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

I use a dictionary to do this
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

Camille1,

Sorry for taking so long to answer your reply.

What I meant was that collections do not allow duplicate keys.  Try this code:

CODE

Sub Sorting()

    '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

(OP)
I learned new things from this.  Thanks to all of you

RE: Sorting string from an array

Camille1,

Just out of curiosity, how did you solve your problem?

Regards,

Regg

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources