+ Reply to Thread
Results 1 to 8 of 8

Function to get unique sorted words

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

    Function to get unique sorted words

    Hi experts
    I have in a cell some words separated by spaces
    e.g
    dog cat tree hair pepper she moon sun cat tree three pepper

    I wanted a function which extracts the unique values and sorts the words by length and alphabetically

    steps to get the result
    first removed the duplicates
    ** dog cat tree hair pepper she moon sun three

    second to sort the words by length

    ** pepper three tree hair moon dog cat

    last step to sort each group of length alphabtically
    ** pepper three hair moon tree cat dog

  2. #2
    Forum Contributor Dante Amor's Avatar
    Join Date
    07-24-2020
    Location
    MEXICO
    MS-Off Ver
    Excel 2013
    Posts
    212

    Re: Function to get unique sorted words

    Use this UDF like:

    =get_unique_sorted(A2)

    Please Login or Register  to view this content.
    Last edited by Dante Amor; 06-11-2022 at 03:49 PM.

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

    Cool Hi, try this ‼


    Quote Originally Posted by KingTamo View Post
    I wanted a function which extracts the unique values and sorts the words by length and alphabetically
    As I yet wrote in your previous thread « the sort can be directly achieved within the Collection »
    so according to the link I gave you in the same thread that's easy as this :

    PHP Code: 
    Function UniqueLengthSortedWords$(ByVal S$)
            
    Dim WN&, V
        With 
    New Collection
            
    For Each W In Split(S)
                
    = Array(WLen(W))
            For 
    1 To .Count
                    V 
    = .Item(N)
                If 
    W(1) > V(1Then
                    
    .Add W, , N:  0:  Exit For
                ElseIf 
    W(0) <= V(0) And W(1) = V(1Then
                    
    If W(0) < V(0Then .Add W, , N
                    N 
    0:  Exit For
                
    End If
            
    Next
                
    If N Then .Add W
            Next
            
    If .Count Then
                UniqueLengthSortedWords 
    = .Item(1)(0)
                For 
    2 To .Count:  UniqueLengthSortedWords UniqueLengthSortedWords " " & .Item(N)(0):  Next
            End 
    If
        
    End With
    End 
    Function

    Sub DemoUniqueLengthSortedWords()
        
    Debug.Print UniqueLengthSortedWords("dog cat tree hair pepper she moon sun cat tree three pepper")
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon « Add Reputation » !

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Function to get unique sorted words

    Quote Originally Posted by KingTamo View Post
    dog cat tree hair pepper she moon sun cat tree three pepper

    ** pepper three hair moon tree cat dog she sun
    Please Login or Register  to view this content.

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

    Re: Function to get unique sorted words

    Thanks a lot sir jindon
    Your code is very excellent

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

    Re: Function to get unique sorted words

    @jindon
    how can I change the code to make sort from the shortest word to the longest one?

  7. #7
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: Function to get unique sorted words

    Just get rid of 1000 -
    Please Login or Register  to view this content.

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

    Re: Function to get unique sorted words

    Thanks a lot sir jindon and sir Marc

+ 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. [SOLVED] Get unique letters sorted
    By KingTamo in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-11-2022, 01:54 PM
  2. [SOLVED] Compare words in 2 columns, formula for unique words in 3rd column
    By thecdnmole in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 04-08-2018, 01:58 PM
  3. Compare words in 2 columns, formula for unique words in 3rd column
    By thecdnmole in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 04-07-2018, 06:26 PM
  4. Assign unique ID to sorted, unique dates
    By Pigeras in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 03-21-2018, 01:09 PM
  5. [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
  6. [SOLVED] Get unique values sorted
    By YasserKhalil in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 01-01-2015, 08:40 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