跳到主要内容

如何在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)))

文档排序单元格中的数字-1

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

文档排序单元格中的数字-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) 放入数据旁边的空白单元格,请参见屏幕截图:

文档排序单元格中的数字-1

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

文档排序单元格中的数字-1

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


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

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

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

文档排序单元格中的数字-1

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

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


相关文章:

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

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

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

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

最佳办公生产力工具

🤖 Kutools 人工智能助手:基于以下内容彻底改变数据分析: 智能执行   |  生成代码  |  创建自定义公式  |  分析数据并生成图表  |  调用 Kutools 函数...
热门特色: 查找、突出显示或识别重复项   |  删除空白行   |  合并列或单元格而不丢失数据   |   不使用公式进行四舍五入 ...
超级查询: 多条件VLookup    多值VLookup  |   跨多个工作表的 VLookup   |   模糊查询 ....
高级下拉列表: 快速创建下拉列表   |  依赖下拉列表   |  多选下拉列表 ....
列管理器: 添加特定数量的列  |  移动列  |  切换隐藏列的可见性状态  |  比较范围和列 ...
特色功能: 网格焦点   |  设计图   |   大方程式酒吧    工作簿和工作表管理器   |  资源库 (自动文本)   |  日期选择器   |  合并工作表   |  加密/解密单元格    按列表发送电子邮件   |  超级筛选   |   特殊过滤器 (过滤粗体/斜体/删除线...)...
前 15 个工具集12 文本 工具 (添加文本, 删除字符,...)   |   50+ 图表 类型 (甘特图,...)   |   40+ 实用 公式 (根据生日计算年龄,...)   |   19 插入 工具 (插入二维码, 从路径插入图片,...)   |   12 转化 工具 (小写金额转大写, 货币兑换,...)   |   7 合并与拆分 工具 (高级组合行, 分裂细胞,...)   |   ... 和更多

使用 Kutools for Excel 增强您的 Excel 技能,体验前所未有的效率。 Kutools for Excel 提供了 300 多种高级功能来提高生产力并节省时间。  单击此处获取您最需要的功能...

产品描述


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

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,发布者,Access,Visio和Project。
  • 在同一窗口的新选项卡中而不是在新窗口中打开并创建多个文档。
  • 每天将您的工作效率提高50%,并减少数百次鼠标单击!
Comments (13)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Hola, se pueden ordenar letras en una sola celda por orden alfabético? Yo uso excel para Mac. Gracias
This comment was minimized by the moderator on the site
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?
This comment was minimized by the moderator on the site
Hello, omer,May be the below code can help you, please try:
<div data-tag="code">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 SubAfter inserting the above code, please apply this formula: =CellSort(A1).And you will get the result you need.
This comment was minimized by the moderator on the site
How to sort A-Z text within a cell in Excel?
This comment was minimized by the moderator on the site
hOLA, MI PROBLEMA ES QUE TENGO EXEL 2019 EN ESPAÑOL COMO SERIA LA FORMULA?
This comment was minimized by the moderator on the site
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?
This comment was minimized by the moderator on the site
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
This comment was minimized by the moderator on the site
need to sort 84-12-74-26-98 any order 12-26-74-84-98 or 98-84-74-26-12 thank you
This comment was minimized by the moderator on the site
If CInt(Arr(xMin)) > CInt(Arr(j)) and it works
This comment was minimized by the moderator on the site
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
This comment was minimized by the moderator on the site
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
This comment was minimized by the moderator on the site
Hi, I was wondering how this UDF, =sortnumsincell(A1,1), can be modified more generally, like =sortnumsincell(A1," "," ",,1) where the first argument, A1, is the target cell, the second argument," ", is a delimiter that could take any character, or a space, or nothing, with third argument, " ", a different or same delimiter, and the fourth argument, 1 or 0, 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.
There are no comments posted here yet
Load More
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations