Note: The other languages of the website are Google-translated. Back to English
登陆  \/ 
x
or
x
注册  \/ 
x

or

如何在Excel中对单元格中的数字进行排序?

对我们而言,在列列表中对数字进行排序很容易并且很常见,但是您是否曾经尝试在单个单元格中对数字进行排序? 也许除了将它们一一排列之外,没有什么好办法供您使用。在这里,我将讨论如何在Excel的单元格中对数字进行排序。

用公式对单元格中的数字排序

使用用户定义的功能对单元格中的数字进行排序

使用VBA代码对单元格中用逗号分隔的数字进行排序


箭头蓝色右气泡 用公式对单元格中的数字排序

要对工作表中单元格中的数字进行排序,您可以应用以下长公式,方法如下:

1。 在您的数据旁边,请输入以下公式,在此示例中,我将其键入到单元格C1中,请参见屏幕截图:

=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. 然后按 Ctrl + Shift + Enter 键,然后将填充手柄拖到要应用此公式的范围内,您将获得数字从小到大的排序。 看截图:

doc-sort-numbers-in-cells-1

备注:

1.如果单元格中数字的位数大于15,则此公式将无法获得正确的结果。

2.如果要按降序对数字进行排序,可以使用以下公式: =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.在上面的公式中,A1表示包含要排序的数字的单元格,您可以根据需要进行更改。


箭头蓝色右气泡 使用用户定义的功能对单元格中的数字进行排序

由于公式存在一些局限性,因此您可以使用以下公式 用户定义功能 对长度超过15位的单元格中的数字进行排序。

1。 按住 ALT + F11 键,然后打开 Microsoft Visual Basic for Applications窗口.

2。 点击 插页 > 模块,然后将以下代码粘贴到 模块窗口.

VBA代码:对单元格内的数字进行排序

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. 然后保存并关闭此代码,返回到工作表,然后输入此公式 = sortnumsincell(A1) 放入数据旁边的空白单元格,请参见屏幕截图:

doc-sort-numbers-in-cells-1

4。 然后将填充手柄拖到要包含此公式的单元格上,并且单元格中的所有数字都已按升序排序,如下所示的屏幕截图:

doc-sort-numbers-in-cells-1

备注:如果要按降序对数字进行排序,请输入此公式 = sortnumsincell(A1,1)。


箭头蓝色右气泡 使用VBA代码对单元格中用逗号分隔的数字进行排序

如果您的数字被某些字符分隔,例如逗号,分号,句号等(如以下屏幕截图所示),那么如何在单元格中对它们进行排序? 现在,我介绍一个VBA代码供您对它们进行排序。

doc-sort-numbers-in-cells-1

1。 按住 ALT + F11 键打开 Microsoft Visual Basic for Applications窗口。

2。 点击 插页 > 模块,然后将以下代码粘贴到 模块窗口.

VBA代码:排序号在单元格中用逗号分隔

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。 然后按 F5 键运行此代码,然后在弹出的提示框中选择包含数字的单元格,请参见屏幕截图:

doc-sort-numbers-in-cells-1

4。 然后点击 OK,则单元格中的所有数字已在原始范围内升序排列。

备注:您可以根据需要在上面的代码中将逗号“,”更改为任何其他字符。 并且此代码只能对数据进行升序排序。


相关文章:

如何在Excel中使用连字符对数字进行排序?

如何在Excel中按最频繁的值对数据进行排序?

如何在Excel中按域对电子邮件地址进行排序?

如何对行进行排序以将空白单元格放在Excel的顶部?


最佳办公效率工具

Kutools for Excel解决了您的大多数问题,并使您的生产率提高了80%

  • 重用: 快速插入 复杂的公式,图表 以及您以前使用过的任何东西; 加密单元 带密码 创建邮件列表 并发送电子邮件...
  • 超级公式栏 (轻松编辑多行文本和公式); 阅读版式 (轻松读取和编辑大量单元格); 粘贴到过滤范围...
  • 合并单元格/行/列 不会丢失数据; 拆分单元格内容; 合并重复的行/列...防止细胞重复; 比较范围...
  • 选择重复或唯一 行; 选择空白行 (所有单元格都是空的); 超级查找和模糊查找 在许多工作簿中; 随机选择...
  • 确切的副本 多个单元格,无需更改公式参考; 自动创建参考 到多张纸; 插入项目符号,复选框等...
  • 提取文字,添加文本,按位置删除, 删除空间; 创建和打印分页小计; 在单元格内容和注释之间转换...
  • 超级滤镜 (将过滤方案保存并应用于其他工作表); 高级排序 按月/周/日,频率及更多; 特殊过滤器 用粗体,斜体...
  • 结合工作簿和工作表; 根据关键列合并表; 将数据分割成多个工作表; 批量转换xls,xlsx和PDF...
  • 超过300种强大功能。 支持Office / Excel 2007-2019和365。支持所有语言。 在您的企业或组织中轻松部署。 完整功能30天免费试用。 60天退款保证。
kte选项卡201905

Office选项卡为Office带来了选项卡式界面,使您的工作更加轻松

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,发布者,Access,Visio和Project。
  • 在同一窗口的新选项卡中而不是在新窗口中打开并创建多个文档。
  • 每天将您的工作效率提高50%,并减少数百次鼠标单击!
officetab底部
Say something here...
symbols left.
You are guest
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.
    omer1905 · 11 months ago
    Hi thanks for nice good for sorting numbers which separated by commas within cells with VBA code

    Just I have faced one problem with the code.

    The code cannot detect three digit number. for example the numbers (65, 93, 53, 72, 64, 85, 103, 48, 77, 81, 54) after applying the code, the new order (103, 48, 53, 54, 64, 65, 72, 77, 81, 85, 93)

    Do you have any solution for the problem?
    • To post as a guest, your comment is unpublished.
      skyyang · 11 months ago
      Hello, omer,
      May be the below code can help you, please try:

      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
      After inserting the above code, please apply this formula: =CellSort(A1).
      And you will get the result you need.
  • To post as a guest, your comment is unpublished.
    Biplab Das · 1 years ago
    How to sort A-Z text within a cell in Excel?
  • To post as a guest, your comment is unpublished.
    WALLAS · 2 years ago
    hOLA, MI PROBLEMA ES QUE TENGO EXEL 2019 EN ESPAÑOL COMO SERIA LA FORMULA?
  • To post as a guest, your comment is unpublished.
    MF · 4 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 · 4 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 · 4 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.
      dd · 1 years ago
      If CInt(Arr(xMin)) > CInt(Arr(j)) and it works
  • To post as a guest, your comment is unpublished.
    Dave · 4 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 · 4 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
  • To post as a guest, your comment is unpublished.
    wschloss · 6 years ago
    Hi, I was wondering how this UDF, [b]=sortnumsincell(A1,1)[/b], can be modified more generally, like [b]=sortnumsincell(A1," "," ",,1)[/b] where the first argument, [b]A1[/b], is the target cell, the second argument,[b]" "[/b], is a delimiter that could take any character, or a space, or nothing, with third argument, [b]" "[/b], a different or same delimiter, and the fourth argument, [b]1 or 0[/b], indicating an ascending or descending sort, with the result string displaying, correctly sorted, within one cell, with delimiter default same as the original string unless specified in the third term. I would like it to work both on string and numerical, and sometimes the second or third argument might be a line feed, as would be manually entered with alt-enter.
    You'd be my hero of the month if you could do that. I tried but failed miserably.
    Thank you.
  • To post as a guest, your comment is unpublished.
    noob · 6 years ago
    sir, could you please help to sort the data for the duplicate first whether it's small or big
    Example
    1211 ---> 1112
    9495 ---> 9945
    7379 ---> 7739

    Thanks a lot for your help