+ Reply to Thread
Results 1 to 4 of 4

Get unique list of strings

Hybrid View

  1. #1
    Registered User
    Join Date
    04-22-2009
    Location
    Boston
    MS-Off Ver
    Excel 2013
    Posts
    68

    Get unique list of strings

    Hi,

    I have 2 arrays of strings.

    Dim str1(1 to 10), str2(1 to 10) as String
    I get str1 filled up using external program. I am filling up those here manually.

    str(1)="Laptop"
    str(2)="MP3 Player"
    str(3)="MP3 Player"
    str(4)="Ipod"
    str(5)="Ipod"
    str(6)="Ipod"
    str(7)="Ipod"
    str(8)="GPS"
    str(9)="TV"
    str(10)=""
    The aim of the program is to get unique names.
    Hence the output will be.

    str2(1)="Laptop"
    str2(2)="MP3 Player"
    str2(3)="Ipod"
    str2(4)="GPS"
    str2(5)="TV"
    str2(6)=""
    str2(7)=""
    str2(8)=""
    str2(9)=""
    str2(10)=""
    It will be also great to minimize the no. of comparisons as I am dealing with strings of quite a good length.

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Get unique list of strings

    Use one array of strings, sort, and delete duplicates?
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Get unique list of strings

    Hello kapilrakh,

    This macro will print the your list of unique strings in a single column on a worksheet and sort them in ascending order.
    Sub ListUniques(Where As Range, ByVal StrArray As Variant)
    
      Dim MyList As Object
      Dim Str As Variant
      
        Set MyList = CreateObject("Scripting.Dictionary")
        MyList.CompareMode = vbTextCompare
        
          For Each Str In StrArray
            If Str <> "" Then
              If Not MyList.Exists(Str) Then MyList.Add Str, 1
            End If
          Next Str
          
          Set Where = Where.Resize(RowSize:=MyList.Count)
          Where = WorksheetFunction.Transpose(MyList.Keys)
          
          Where.Sort Key1:=Where.Cells(1, 1), Order1:=xlAscending, Header:=xlNo, _
                     MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
    
        Set MyList = Nothing
          
    End Sub
    Example of Using the Macro
    This will copy the list to the Active Sheet. Starting in Cell "A1".
        ListUniques Range("A1"), Str1
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  4. #4
    Valued Forum Contributor
    Join Date
    10-15-2007
    Location
    Home
    MS-Off Ver
    Office 2010, W10
    Posts
    373

    Re: Get unique list of strings

    Hi kapilrakh

    If you have excel 2007 an easy way to do it is to use an auxilliary range and the Range method RemoveDuplicates.

    Ex.: using column A in the active sheet as auxilliary:

    Function RemoveDups(str1)
    
    With Range("A1").Resize(UBound(str1))
        .Value = Application.Transpose(str1)
        .RemoveDuplicates Columns:=1, Header:=xlNo
        RemoveDups = Application.Transpose(.Value)
    End With
    End Function

    This funcion receives an array of strings and returns an array with the same size but without duplicates.

    Notice that you don't have to worry with any algorithm.

    Let's test it with the example you posted:

    Sub a()
    Dim str1(1 To 10), str2
    
    str1(1) = "Laptop"
    str1(2) = "MP3 Player"
    str1(3) = "MP3 Player"
    str1(4) = "Ipod"
    str1(5) = "Ipod"
    str1(6) = "Ipod"
    str1(7) = "Ipod"
    str1(8) = "GPS"
    str1(9) = "TV"
    str1(10) = ""
    
    str2 = RemoveDups(str1)
    MsgBox Join(str2, ", ")
    End Sub
    Remark: I assumed the array index starts at 1.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1