+ Reply to Thread
Results 1 to 4 of 4

Sorting values within cell alphabetically

Hybrid View

  1. #1
    Registered User
    Join Date
    07-16-2019
    Location
    New Zealand
    MS-Off Ver
    365
    Posts
    44

    Sorting values within cell alphabetically

    Hi Excel Forum,

    I have a column of cells with months that are out of order.

    E.g: "January October November December March April May February June July August September"

    Does anyone know of a quick way to sort these by month?

    I found this user defined function which sorts by number:

    Public Function CellSort(r As Range) As String
        Dim bry() As Long, L As Long, U As Long
        ch = r(1).Text
        ary = Split(ch, ",")
        L = LBound(ary)
        U = UBound(ary)
        ReDim bry(L To U)
        For i = LBound(ary) To UBound(ary)
            bry(i) = CLng(ary(i))
        Next i
    
        Call BubbleSort(bry)
    
        For i = LBound(bry) To UBound(bry)
            ary(i) = CStr(bry(i))
        Next i
        CellSort = Join(ary, ",")
    End Function
    
    
    Sub BubbleSort(arr)
        Dim strTemp As Variant
        Dim i As Long
        Dim j As Long
        Dim lngMin As Long
        Dim lngMax As Long
        lngMin = LBound(arr)
        lngMax = UBound(arr)
        For i = lngMin To lngMax - 1
            For j = i + 1 To lngMax
                If arr(i) > arr(j) Then
                    strTemp = arr(i)
                    arr(i) = arr(j)
                    arr(j) = strTemp
                End If
            Next j
        Next i
    End Sub
    https://superuser.com/questions/7051...-cell-in-excel

    Many Thanks

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    5,983

    Re: Sorting values within cell alphabetically

    How about this, for a list of months in A2:

    =LET(v,TEXTSPLIT(A2," "),d,DATEVALUE(v & " 1, 2023"),s,SORT(TRANSPOSE(d),1),TEXTJOIN(" ",FALSE,TEXT(TRANSPOSE(s),"mmmm")))

    You can then copy the formulas and paste values over the original cells.

    Ooops - sorry. Getting too used to everyone having 365....

    Change your function to this:

    Public Function CellSort(r As Range) As String
        Dim bry() As Long, L As Long, U As Long
        ch = r(1).Text
        ary = Split(ch, " ")
        L = LBound(ary)
        U = UBound(ary)
        ReDim bry(L To U)
        For i = LBound(ary) To UBound(ary)
            bry(i) = CDate(ary(i) & " 1, 2023")  ' year does not matter
        Next i
    
        Call BubbleSort(bry)
    
        For i = LBound(bry) To UBound(bry)
            ary(i) = Format(CStr(bry(i)), "mmmm")
        Next i
        CellSort = Join(ary, " ")
    End Function
    Last edited by Bernie Deitrick; 01-19-2023 at 04:40 PM.
    Bernie Deitrick
    Excel MVP 2000-2010

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

    Re: Sorting values within cell alphabetically

    try
    Function CellSort(s As String) As String
        Dim x(2)
        x(0) = Split(Application.Trim(s))
        x(1) = [transpose(text(row(1:12)*29,"mmm""*"""))]
        x(2) = Filter(Application.IfError(Application.Match(x(1), x(0), 0), Chr(2)), Chr(2), 0)
        CellSort = Join(Application.Index(x(0), x(2)))
    End Function

  4. #4
    Registered User
    Join Date
    07-16-2019
    Location
    New Zealand
    MS-Off Ver
    365
    Posts
    44

    Re: Sorting values within cell alphabetically

    Thank you very much Bernie and Jindon! Both of these responses work perfectly

+ 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. Sorting Data Alphabetically In One Cell Using VBA - Not Removing Spaces
    By thechrissamsa in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-17-2021, 01:14 PM
  2. Replies: 0
    Last Post: 01-17-2021, 12:39 PM
  3. [SOLVED] Sorting data alphabetically in one cell by surname
    By Jasper1 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-14-2020, 04:30 AM
  4. Sorting a list alphabetically keeping duplicates+ lookup some values
    By buyshirts in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 11-24-2015, 04:38 AM
  5. [SOLVED] Return column heading of top 5 values, sorting alphabetically if there's a tie
    By abullock in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 12-16-2013, 02:07 AM
  6. Help in Sorting Unknown Cell Range Alphabetically
    By crisshinn in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-21-2011, 07:01 AM
  7. Sorting data in a cell alphabetically
    By Newbie1092 in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 12-28-2005, 01:55 PM

Tags for this Thread

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