跳到主要內容

如何在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的頂部?

最佳辦公生產力工具

熱門特色: 尋找、突出顯示或識別重複項   |  刪除空白行   |  合併列或儲存格而不遺失數據   |   沒有公式的回合 ...
超級查詢: 多條件VLookup    多值VLookup  |   跨多個工作表的 VLookup   |   模糊查詢 ....
高級下拉列表: 快速建立下拉列表   |  依賴下拉列表   |  多選下拉列表 ....
欄目經理: 新增特定數量的列  |  移動列  |  切換隱藏列的可見性狀態  |  比較範圍和列 ...
特色功能: 網格焦點   |  設計圖   |   大方程式酒吧    工作簿和工作表管理器   |  資源庫 (自動文字)   |  日期選擇器   |  合併工作表   |  加密/解密單元格    按清單發送電子郵件   |  超級濾鏡   |   特殊過濾器 (過濾粗體/斜體/刪除線...)...
前 15 個工具集12 文本 工具 (添加文本, 刪除字符,...)   |   50+ 圖表 類型 (甘特圖,...)   |   40+ 實用 公式 (根據生日計算年齡,...)   |   19 插入 工具 (插入二維碼, 從路徑插入圖片,...)   |   12 轉化 工具 (數字到單詞, 貨幣兌換,...)   |   7 合併與拆分 工具 (高級合併行, 分裂細胞,...)   |   ... 和更多

使用 Kutools for Excel 增強您的 Excel 技能,體驗前所未有的效率。 Kutools for Excel 提供了 300 多種進階功能來提高生產力並節省時間。  點擊此處獲取您最需要的功能...

kte選項卡201905


Office選項卡為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