Tip: Other languages are Google-Translated. You can visit the English version of this link.
Log in
x
or
x
x
Register
x

or

How to sort numbers within a cell in Excel?

It is easy and common for us to sort numbers in a list of column, but have you ever tried to sort numbers within a single cell? May be there is no good way for you except arrange them one by one, here, I will talk about how to sort numbers within cells in Excel.

Sort numbers within cells with formula

Sort numbers within cells with User Defined Function

Sort numbers which separated by commas within cells with VBA code

Excel Productivity Tools

Office Tab: Bring powerful tabs to Office (include Excel), just like Chrome, Safari, Firefox and Internet Explorer. Save you half the time, and reduce thousands of mouse clicks for you. 30-day Unlimited Free Trial

Kutools for Excel: Save 70% of your time and solve 80% Excel problems for you. 300+ advanced features designed for 1500+ work scenario, make Excel much easy and increase productivity immediately. 60-day Unlimited Free Trial


arrow blue right bubble Sort numbers within cells with formula


To sort numbers within cells in a worksheet, you can apply the following long formula, please do as this:

1. Next to your data, please enter the following formula, in this example, I will type it into cell C1, see screenshot:

=TEXT(SUM(SMALL(--MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1),ROW(INDIRECT("1:"&LEN(A1))))*10^(LEN(A1)-ROW(INDIRECT("1:"&LEN(A1))))),REPT("0",LEN(A1)))

doc-sort-numbers-in-cells-1

2. Then press Ctrl + Shift + Enter keys together, then drag the fill handle over to the range that you want to apply this formula, and you will get the numbers have been sorted from small to large. See screenshot:

doc-sort-numbers-in-cells-1

Notes:

1. If the digit of the number is more than 15 in the cell, this formula will not get the correct result.

2. If you want to sort the numbers in descending order, you can use this formula: =TEXT(SUM(LARGE(--MID(A1,ROW(INDIRECT("1:"&LEN(A1))),1),ROW(INDIRECT("1:"&LEN(A1))))*10^(LEN(A1)-ROW(INDIRECT("1:"&LEN(A1))))),REPT("0",LEN(A1))).

3. In the above formulas, A1 indicates the cell which contains the numbers you want to sort, you can change it to your need.


arrow blue right bubble Sort numbers within cells with User Defined Function

As there are some limitations of the formula, you can use the following User Defined Function to sort numbers in cells longer than 15 digits.

1. Hold down the ALT + F11 keys, and it opens the Microsoft Visual Basic for Applications window.

2. Click Insert > Module, and paste the following code in the Module Window.

VBA code: Sort numbers within cells

Function SortNumsInCell(pNum As String, Optional pOrder As Boolean) As String
'Update 20140717
Dim xOutput As String
For i = 0 To 9
  For j = 1 To UBound(VBA.Split(pNum, i))
    xOutput = IIf(pOrder, i & xOutput, xOutput & i)
  Next
Next
SortNumsInCell = xOutput
End Function

3. Then save and close this code, go back to your worksheet, and enter this formula =sortnumsincell(A1) into a blank cell next to your data, see screenshot:

doc-sort-numbers-in-cells-1

4. And then drag the fill handle to the cells that you want to contain this formula, and all the numbers in the cells have been sorted in ascending order as following screenshot shown:

doc-sort-numbers-in-cells-1

Note: If you want to sort the numbers in descending order, please enter this formula =sortnumsincell(A1,1).


arrow blue right bubble Sort numbers which separated by commas within cells with VBA code

If your numbers are separated by certain characters such as comma, semicolon, period and so on as following screenshot, how could you sort them in cells? Now, I introduce a VBA code for you to sort them.

doc-sort-numbers-in-cells-1

1. Hold down the ALT + F11 keys to open the Microsoft Visual Basic for Applications window.

2. Click Insert > Module, and paste the following code in the Module Window.

VBA code: Sort numbers are separated by commas within cells

Sub SortNumsInRange()
'Update 20140717
Dim Rng As Range
Dim WorkRng As Range
Dim Arr As Variant
On Error Resume Next
xTitleId = "KutoolsforExcel"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
Set objArrayList = CreateObject("System.Collections.ArrayList")
For Each Rng In WorkRng
    Arr = VBA.Split(Rng.Value, ",")
    For i = 0 To UBound(Arr)
        xMin = i
        For j = i + 1 To UBound(Arr)
            If Arr(xMin) > Arr(j) Then
                xMin = j
            End If
        Next j
        If xMin <> i Then
            temp = Arr(i)
            Arr(i) = Arr(xMin)
            Arr(xMin) = temp
        End If
    Next i
    Rng.Value = VBA.Join(Arr, ",")
Next
End Sub

3. Then press F5 key to run this code, and then select your cells which contain the numbers in the popped out prompt box, see screenshot:

doc-sort-numbers-in-cells-1

4. And then click OK, all the numbers in the cells have been sorted ascendingly in the original range.

Note: You can change the comma “,” to any other characters as you need in the above code. And this code only can sort data ascendingly.


Related articles:

How to sort numbers with hyphens in Excel?

How to sort data by the most frequent value in Excel?

How to sort email address by domain in Excel?

How to sort rows to put the blank cells on top in Excel?


Excel Productivity Tools

Ribbon of Excel (with Kutools for Excel installed)

300+ Advanced Features Increase Your Productivity by 70%, and Help You To Stand Out From Crowd!

Would you like to complete your daily work quickly and perfectly? Kutools For Excel brings 300+ cool and powerful advanced features (Combine workbooks, sum by color, split cell contents, convert date, and so on...) for 1500+ work scenarios, helps you solve 80% Excel problems.

  • Deal with all complicated tasks in seconds, help to enhance your work ability, get success from the fierce competition, and never worry about being fired.
  • Save a lot of work time, leave much time for you to love and care the family and enjoy a comfortable life now.
  • Reduce thousands of keyboard and mouse clicks every day, relieve your tired eyes and hands, and give you a healthy body.
  • Become an Excel expert in 3 minutes, and get admiring glance from your colleagues or friends.
  • No longer need to remember any painful formulas and VBA codes, have a relaxing and pleasant mind, give you a thrill you've never had before.
  • Spend only $39, but worth than $4000 training of others. Being used by 110,000 elites and 300+ well-known companies.
  • 60-day unlimited free trial. 60-day money back guarantee. Free upgrade and support for 2 years. Buy once, use forever.
  • Change the way you work now, and give you a better life immediately!

Office Tab Brings Efficient And Handy Tabs to Office (include Excel), Just Like Chrome, Firefox, And New IE

  • Increases your productivity by 50% when viewing and editing multiple documents.
  • Reduce hundreds of mouse clicks for you every day, say goodbye to mouse hand.
  • Open and create documents in new tabs of same window, rather than in new windows.
  • Help you work faster and easily stand out from the crowd! One second to switch between dozens of open documents!
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Micael · 1 years ago
    Hi, the VBA code seems to output incorrectly, example before 13,50,47,7,39 and after 13-39-47-50-7. Any ideas why?
  • To post as a guest, your comment is unpublished.
    Jijoy P Jacob · 1 years ago
    i want to sort total an amount 14000 to 20000 from various row Example:- 2000,1500 one row and like that all row amount to arrange
  • To post as a guest, your comment is unpublished.
    orly · 1 years ago
    need to sort 84-12-74-26-98 any order 12-26-74-84-98 or 98-84-74-26-12
    thank you
  • To post as a guest, your comment is unpublished.
    Dave · 1 years ago
    Is there is any way to sort more numbers in same time from one cell? Example, i have a list of 50000 asset numbers such as A1234,A1235... and i need to pull 500 specific numbers and i need to pull 500 at the time to make change and save.Thank you
  • To post as a guest, your comment is unpublished.
    Brian Dolan · 2 years ago
    I have a series of cells with numbers separated by a space that I want to sort.
    eg 8 4 5 1 6 3 that I want to sort as 1 3 4 5 6 8
    Any help appreciated