Sort number data within a cell

sscls

New Member
Joined
Oct 26, 2011
Messages
3
[h=2]Sorting Data (Numbers) Within a Single Cell[/h]
I'm using a spreadsheet to input a series of digits into a particular cell. I want to be able to use formula 。
For example, I input the numbers: 21, 10, 37, 2, 5, 44 into cell A1; the numbers will be separted by a space not a comma (,).
After formula, cell B2 (or whatever cell) should show: 2 5 10 21 37 44. Please help!

if I input the numbers: 21,21, 10, 37, 2, 2,5, 44,37 into cell A1, the B1 should also cannot duplicated ,so should still be 2 5 10 21 37 44


Regards.

i use the url https://www.ozgrid.com/forum/forum/...298-sorting-data-numbers-within-a-single-cell
but the different is
1.to Remove duplicate numbers
2.use real formula not vba
3.if use VBA how to?
thanks a lot
 

Some videos you may like

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
36,041
Office Version
2010
Platform
Windows
Here is a VBA solution... it is a UDF (user defined function)... see installation instructions below:
Code:
Function SortNumbers(Text As String) As String
  Dim X As Long, Max As Long, Data As Variant
  Text = Replace(Application.Trim(Replace(Text, ",", " ")), " ", ",")
  Max = Evaluate("MAX(" & Text & ")")
  Data = Split(Text, ",")
  With CreateObject("System.Collections.ArrayList")
    For X = 0 To UBound(Data)
      .Add Application.Trim(Format(Data(X), String(Len(Max), "0")))
    Next
    .Sort
    Data = .ToArray
  End With
  With CreateObject("Scripting.Dictionary")
    For X = 0 To UBound(Data)
      .Item(CLng(Data(X))) = 1
    Next
    SortNumbers = Join(.keys)
  End With
End Function
HOW TO INSTALL UDFs
------------------------------------
If you are new to UDFs, they are easy to install and use. To install it, simply press ALT+F11 to go into the VB editor and, once there, click Insert/Module on its menu bar, then copy/paste the above code into the code window that just opened up. That's it.... you are done. You can now use SortNumbers just like it was a built-in Excel function. For example, if cell A1 contains your number list, then put this in cell B1...

=SortNumbers(A1)

If you are using XL2007 or above, make sure you save your file as an "Excel Macro-Enabled Workbook (*.xlsm) and answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,202
Office Version
2007
Platform
Windows
Just another UDF

Code:
Function Sorting_Num(r As Range)
    Dim n As New Collection, e As Boolean, cad As String, i As Long, c As Variant
    On Error Resume Next
    For Each c In Application.Trim(Split(r.Value, ","))
        e = False
        For i = 1 To n.Count
            If Val(c) < n(i) Then n.Add Val(c), c, i: e = True
        Next
        If e = False Then n.Add Val(c), c
    Next
    For i = 1 To n.Count
        cad = cad & n(i) & " "
    Next
    Sorting_Num = Left(cad, Len(cad) - 1)
End Function
 

lrobbo314

Well-known Member
Joined
Jul 14, 2008
Messages
2,772
Office Version
365, 2019, 2016
Platform
Windows
Yet another way.

Code:
Function Sorted(val As String) As String
val = Replace(val, " ", "")
Dim s() As String: s = Split(val, ",")
Dim AL As Object: Set AL = CreateObject("System.Collections.ArrayList")
Dim tmp As Long

For i = LBound(s) To UBound(s)
    tmp = s(i)
    If Not AL.contains(tmp) Then AL.Add tmp
Next i

AL.Sort
Sorted = Join(AL.toArray, " ")

End Function
 
Last edited:

Watch MrExcel Video

Forum statistics

Threads
1,102,476
Messages
5,487,109
Members
407,579
Latest member
chhantelravi

This Week's Hot Topics

Top