Sort range of values within a cell

Chlwls808

Board Regular
Joined
Jun 20, 2021
Messages
51
Office Version
  1. 2016
Platform
  1. Windows
Hi,
A bit complex question..

Because Excel don't allow sorting values inside one cell, I'm trying to use a UDF to handle the sorting. The below module is suppose to help me do it in alphabetical order of a given list of items contained in a cell.

VBA Code:
Function SortWithinCell(CelltoSort As Range, DelimitingCharacter As String, IncludeSpaces As Boolean) As String
CelltoSortString = WorksheetFunction.Substitute(CelltoSort.Value, " ", "")
MyArray = Split(CelltoSortString, DelimitingCharacter)
    For N = 0 To UBound(MyArray)
        For M = 1 To UBound(MyArray)
            
                If MyArray(M) < MyArray(M - 1) Then
                    TempValue = MyArray(M)
                    MyArray(M) = MyArray(M - 1)
                    MyArray(M - 1) = TempValue
                End If

        Next M
    Next N
For N = 0 To UBound(MyArray)
    SortWithinCell = SortWithinCell & MyArray(N) & DelimitingCharacter
Next N
SortWithinCell = Left(SortWithinCell, Len(SortWithinCell) - 1)
If IncludeSpaces = True Then SortWithinCell = WorksheetFunction.Substitute(SortWithinCell, ",", ", ")
End Function

But suppose you have a list that looks like this:

33AA21 33AB21 34AC32 36AE12 36AF12

The main goal is to sort it numerically based on first two digit - Highest to lowest, then alphabetically AA to ZZ.. the next numbers aren't relevant.

Appreciate any feedback on the above.

Thanks!
 
;)

VBA Code:
Function jec(cell As String, delimiter As String) As String
  With CreateObject("system.collections.arraylist")
    For Each it In Split(cell, delimiter)
      .Add it
    Next
      .Sort
      .Reverse
      jec = Join(.toarray)
  End With
End Function

Very close, the output on the function comes out to:
1630088254170.png


which wasn't quite what I am looking for. The alphabetical order must remain as is.

expected outcome:
36AE12 36AF12 34AC32 33AA21 33AB21
 
Upvote 0

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Would it be too difficult to tweak it a little to sort numbers highest to lowest, while keeping the Alphabetical order as is?
It is certainly not a simple "tweak" as you would need to split the 1st 2 characters from each string, whilst keeping them "tied" to the original string.
 
Upvote 0
Shame the OP doesn't have 365, then it could be done quite easily with a formula.
 
Upvote 0
@Fluff did you manage to produce the desired outcome with an office 365 formula?
I think I have it like this:

Excel Formula:
=SORT(--LEFT(MID(H23,{1;8;15;22;29},6),2),,-1)&SORT(RIGHT(MID(H23,{1;8;15;22;29},6),4),,,-1)
 
Last edited:
Upvote 0
Bit of a bodge, but how about
VBA Code:
Function Chlwls(Txt As String, Delim As String) As String
   Dim Sp As Variant, Sp2 As Variant, Tmp As Variant
   Dim i As Long, j As Long
   
   Sp = Split(Txt, Delim)
   ReDim Sp2(UBound(Sp))
   For i = 0 To UBound(Sp)
      Sp2(i) = Left(Sp(i), 2) & UBound(Sp) - i
   Next i
   For i = 0 To UBound(Sp2) - 1
      For j = i + 1 To UBound(Sp2)
         If Sp2(j) > Sp2(i) Then
            Tmp = Sp2(i): Sp2(i) = Sp2(j): Sp2(j) = Tmp
            Tmp = Sp(i): Sp(i) = Sp(j): Sp(j) = Tmp
         End If
      Next j
   Next i
   Chlwls = Join(Sp, Delim)
End Function
 
Upvote 0
Ignore the previous code, it won't work if you have more than 9 substrings.
Try this instead
VBA Code:
Function Chlwls(txt As String, Delim As String) As String
   Dim Sp As Variant, Sp2 As Variant, Tmp As Variant
   Dim i As Long, j As Long
   
   Sp = Split(txt, Delim)
   ReDim Sp2(UBound(Sp))
   For i = 0 To UBound(Sp)
      Sp2(i) = Left(Sp(i), 2) & Chr(UBound(Sp) - i + 65)
   Next i
   For i = 0 To UBound(Sp2) - 1
      For j = i + 1 To UBound(Sp2)
         If Sp2(j) > Sp2(i) Then
            Tmp = Sp2(i): Sp2(i) = Sp2(j): Sp2(j) = Tmp
            Tmp = Sp(i): Sp(i) = Sp(j): Sp(j) = Tmp
         End If
      Next j
   Next i
   Chlwls = Join(Sp, Delim)
End Function
 
Upvote 0
Solution
Ignore the previous code, it won't work if you have more than 9 substrings.
Try this instead
VBA Code:
Function Chlwls(txt As String, Delim As String) As String
   Dim Sp As Variant, Sp2 As Variant, Tmp As Variant
   Dim i As Long, j As Long
  
   Sp = Split(txt, Delim)
   ReDim Sp2(UBound(Sp))
   For i = 0 To UBound(Sp)
      Sp2(i) = Left(Sp(i), 2) & Chr(UBound(Sp) - i + 65)
   Next i
   For i = 0 To UBound(Sp2) - 1
      For j = i + 1 To UBound(Sp2)
         If Sp2(j) > Sp2(i) Then
            Tmp = Sp2(i): Sp2(i) = Sp2(j): Sp2(j) = Tmp
            Tmp = Sp(i): Sp(i) = Sp(j): Sp(j) = Tmp
         End If
      Next j
   Next i
   Chlwls = Join(Sp, Delim)
End Function
Man, you guys are amazing. It works like a charm! Thank you so much! I'd love to be good at this as you one day.
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,632
Latest member
jladair

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top