+ Reply to Thread
Results 1 to 8 of 8

Function to get unique sorted words

Hybrid View

  1. #1
    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)

    Option Explicit
    
    Dim UniqueItms As New Collection
    
    Function get_unique_sorted(s As String)
      Dim i As Long, j As Long, k As Long, m As Long, nWords As Long, lenMax As Long
      Dim w As Variant, arr As Variant, b As Variant, c As Variant, t As Variant
      Dim s1 As String
      
      arr = Split(s, " ")
      nWords = UBound(arr) + 1
      For Each w In arr
        If Len(w) > lenMax Then lenMax = Len(w)
      Next
      ReDim b(1 To lenMax, 1 To nWords)
      ReDim c(1 To lenMax * nWords, 1 To 1)
      
      s = Left(s, Len(s))
      For Each w In Split(s, " ")
        i = Len(w)
        For j = 1 To UBound(b, 2)
          If b(i, j) = "" Then
            b(i, j) = w
            Exit For
          End If
        Next
      Next
      
      m = 1
      For i = UBound(b, 1) To 1 Step -1
        Set UniqueItms = Nothing
        For j = 1 To UBound(b, 2)
          If b(i, j) = "" Then Exit For
          Call AddWord(b(i, j))
        Next
        
        For Each t In UniqueItms
          c(m, 1) = t
          m = m + 1
        Next
      Next
      get_unique_sorted = Join(Application.Transpose(c), " ")
    End Function
    
    Sub AddWord(itm)
      Dim x As Long
      For x = 1 To UniqueItms.Count
        Select Case StrComp(UniqueItms(x), itm, vbTextCompare)
          Case 0: Exit Sub
          Case 1: UniqueItms.Add itm, Before:=x: Exit Sub
        End Select
      Next
      UniqueItms.Add itm
    End Sub
    Last edited by Dante Amor; 06-11-2022 at 03:49 PM.

+ 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