+ Reply to Thread
Results 1 to 8 of 8

Get unique letters sorted

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-24-2016
    Location
    Libya
    MS-Off Ver
    Office 365
    Posts
    526

    Get unique letters sorted

    Hi excel experts

    Searching google, I finded the function which get unique letters in cell
    Function UniqueChars(ByVal origString As String) As String
        Dim oCol As New Collection, sAns As String, sChar As String, lCtr As Long, lCount As Long
        lCount = Len(origString)
        For lCtr = 1 To lCount
            sChar = Mid(origString, lCtr, 1)
            On Error Resume Next
            oCol.Add sChar, sChar
            If Err.Number = 0 Then sAns = sAns & sChar
            Err.Clear
        Next lCtr
        UniqueChars = sAns
    End Function
    The function is very good, but how I get the result sortted?

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,634

    Re: Get unique letters sorted

    See: https://stackoverflow.com/questions/...ction-by-value
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,626

    Re: Get unique letters sorted

    Try
    Function UniqueSorted(IP As String)
    Dim L As Long, T As Long, Ta As Long, K As Long
    Dim A(1 To 26)
    Dim S As String, Strg As String, Rslt As String
    
    L = Len(IP)
    For T = 1 To L
    S = LCase(Mid(IP, T, 1))
    If InStr(1, Strg, S) = 0 Then
    Strg = Strg & S
    K = InStr(1, "abcdefghijklmnopqrstuvwxyz", S)
    A(K) = Mid(IP, T, 1)
    End If
    Next T
    
    For Ta = 1 To 26
    Rslt = Rslt & A(Ta)
    Next Ta
    UniqueSorted = Rslt
    End Function
    Attached Files Attached Files
    Pl note
    Array formula should be confirmed with Ctrl+Shift+Enter keys together.
    If answere is satisfactory press * to add reputation.

  4. #4
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,626

    Re: Get unique letters sorted

    Improved code
    Function UniqueSorted(IP As String)
    Dim L As Long, T As Long, Ta As Long, K As Long
    Dim A(1 To 26)
    Dim S As String, Rslt As String
    
    L = Len(IP)
    For T = 1 To L
    S = LCase(Mid(IP, T, 1))
    K = InStr(1, "abcdefghijklmnopqrstuvwxyz", S)
    If A(K) = "" Then A(K) = Mid(IP, T, 1)
    Next T
    
    For Ta = 1 To 26
    Rslt = Rslt & A(Ta)
    Next Ta
    UniqueSorted = Rslt
    End Function

  5. #5
    Forum Expert
    Join Date
    10-11-2021
    Location
    Netherlands
    MS-Off Ver
    365
    Posts
    1,387

    Re: Get unique letters sorted

    You can sort a collections.arraylist

    Function jec(str As String) As String
     Dim x, i As Long
     With CreateObject("system.collections.arraylist")
       For i = 1 To Len(str)
         x = Mid(str, i, 1)
         If Not .contains(x) Then .Add x
       Next
       .Sort
       jec = Join(.toarray, "")
     End With
    End Function

  6. #6
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Get unique letters sorted


    Quote Originally Posted by KingTamo View Post
    The function is very good, but how I get the result sortted?
    Hi,

    as just reading the VBA help the sort can be directly achieved within the Collection when adding an item !
    Like you can see in this thread

  7. #7
    Forum Contributor
    Join Date
    11-24-2016
    Location
    Libya
    MS-Off Ver
    Office 365
    Posts
    526

    Re: Get unique letters sorted

    Thanks a lot to all of you
    You are very helpful

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,634

    Re: Get unique letters sorted

    You're welcome.



    If you are satisfied with the solution(s) provided, please mark your thread as Solved.


    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save


    You may also want to consider thanking those people who helped you by clicking on the little star at the bottom left of their reply to your question.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 1
    Last Post: 12-23-2021, 07:25 AM
  2. Assign unique ID to sorted, unique dates
    By Pigeras in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 03-21-2018, 01:09 PM
  3. [SOLVED] Sorted and unique validation list
    By YasserKhalil in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 01-23-2016, 05:20 PM
  4. [SOLVED] Get unique sorted combobox on userfrom
    By YasserKhalil in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-19-2015, 06:06 AM
  5. [SOLVED] Get unique values sorted
    By YasserKhalil in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 01-01-2015, 08:40 AM
  6. Replies: 1
    Last Post: 08-27-2014, 11:05 AM
  7. Sorted List of all Unique strings
    By ElmerS in forum Excel General
    Replies: 2
    Last Post: 04-30-2009, 02:40 PM

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