ไปยังเนื้อหาหลัก

วิธีจัดเรียงตัวเลขภายในเซลล์ใน Excel

เป็นเรื่องง่ายและเป็นเรื่องปกติที่เราจะจัดเรียงตัวเลขในรายการคอลัมน์ แต่คุณเคยพยายามจัดเรียงตัวเลขภายในเซลล์เดียวหรือไม่? อาจไม่มีวิธีที่ดีสำหรับคุณยกเว้นจัดเรียงทีละรายการที่นี่ฉันจะพูดถึงวิธีการเรียงลำดับตัวเลขภายในเซลล์ใน Excel

จัดเรียงตัวเลขภายในเซลล์ด้วยสูตร

จัดเรียงตัวเลขภายในเซลล์ด้วย User Defined Function

จัดเรียงตัวเลขที่คั่นด้วยเครื่องหมายจุลภาคภายในเซลล์ด้วยรหัส 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-ตัวเลขในเซลล์-1

2. จากนั้นกด Ctrl + Shift + Enter เข้าด้วยกันจากนั้นลากจุดจับเติมไปยังช่วงที่คุณต้องการใช้สูตรนี้และคุณจะได้รับตัวเลขที่เรียงลำดับจากเล็กไปหาใหญ่ ดูภาพหน้าจอ:

doc-sort-ตัวเลขในเซลล์-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 ระบุเซลล์ที่มีตัวเลขที่คุณต้องการจัดเรียงคุณสามารถเปลี่ยนได้ตามต้องการ


ลูกศรสีฟ้าฟองขวา จัดเรียงตัวเลขภายในเซลล์ด้วย User Defined Function

เนื่องจากมีข้อ จำกัด บางประการของสูตรคุณสามารถใช้สิ่งต่อไปนี้ ฟังก์ชันที่ผู้ใช้กำหนด เพื่อจัดเรียงตัวเลขในเซลล์ที่ยาวกว่า 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-ตัวเลขในเซลล์-1

4. จากนั้นลากจุดจับเติมไปยังเซลล์ที่คุณต้องการให้มีสูตรนี้และตัวเลขทั้งหมดในเซลล์จะถูกเรียงลำดับจากน้อยไปมากตามภาพหน้าจอต่อไปนี้:

doc-sort-ตัวเลขในเซลล์-1

หมายเหตุ: หากคุณต้องการเรียงลำดับตัวเลขจากมากไปหาน้อยโปรดป้อนสูตรนี้ = sortnumsincell (A1,1)


ลูกศรสีฟ้าฟองขวา จัดเรียงตัวเลขที่คั่นด้วยเครื่องหมายจุลภาคภายในเซลล์ด้วยรหัส VBA

หากตัวเลขของคุณถูกคั่นด้วยอักขระบางตัวเช่นลูกน้ำอัฒภาคเครื่องหมายจุดและอื่น ๆ ตามภาพหน้าจอต่อไปนี้คุณจะเรียงลำดับในเซลล์ได้อย่างไร ตอนนี้ขอแนะนำรหัส VBA เพื่อให้คุณเรียงลำดับ

doc-sort-ตัวเลขในเซลล์-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-ตัวเลขในเซลล์-1

4. จากนั้นคลิก OKตัวเลขทั้งหมดในเซลล์จะเรียงลำดับจากน้อยไปมากในช่วงเดิม

หมายเหตุ: คุณสามารถเปลี่ยนเครื่องหมายจุลภาค“,” เป็นอักขระอื่น ๆ ตามที่คุณต้องการในโค้ดด้านบน และรหัสนี้สามารถจัดเรียงข้อมูลจากน้อยไปมากเท่านั้น


บทความที่เกี่ยวข้อง:

วิธีจัดเรียงตัวเลขด้วยยัติภังค์ใน Excel

วิธีจัดเรียงข้อมูลตามค่าที่พบบ่อยที่สุดใน Excel?

วิธีจัดเรียงที่อยู่อีเมลตามโดเมนใน Excel

วิธีจัดเรียงแถวเพื่อวางเซลล์ว่างไว้ด้านบนใน Excel

สุดยอดเครื่องมือเพิ่มผลผลิตในสำนักงาน

🤖 Kutools AI ผู้ช่วย: ปฏิวัติการวิเคราะห์ข้อมูลโดยยึดตาม: การดำเนินการที่ชาญฉลาด   |  สร้างรหัส  |  สร้างสูตรที่กำหนดเอง  |  วิเคราะห์ข้อมูลและสร้างแผนภูมิ  |  เรียกใช้ฟังก์ชัน Kutools...
คุณสมบัติยอดนิยม: ค้นหา เน้น หรือระบุรายการที่ซ้ำกัน   |  ลบแถวว่าง   |  รวมคอลัมน์หรือเซลล์โดยไม่สูญเสียข้อมูล   |   รอบโดยไม่มีสูตร ...
การค้นหาขั้นสูง: VLookup หลายเกณฑ์    VLookup หลายค่า  |   VLookup ข้ามหลายแผ่น   |   การค้นหาที่ไม่ชัดเจน ....
รายการแบบเลื่อนลงขั้นสูง: สร้างรายการแบบหล่นลงอย่างรวดเร็ว   |  รายการแบบหล่นลงขึ้นอยู่กับ   |  เลือกหลายรายการแบบหล่นลง ....
ผู้จัดการคอลัมน์: เพิ่มจำนวนคอลัมน์เฉพาะ  |  ย้ายคอลัมน์  |  สลับสถานะการมองเห็นของคอลัมน์ที่ซ่อนอยู่  |  เปรียบเทียบช่วงและคอลัมน์ ...
คุณสมบัติเด่น: กริดโฟกัส   |  มุมมองการออกแบบ   |   บาร์สูตรใหญ่    สมุดงานและตัวจัดการชีต   |  ห้องสมุดทรัพยากร (ข้อความอัตโนมัติ)   |  เลือกวันที่   |  รวมแผ่นงาน   |  เข้ารหัส/ถอดรหัสเซลล์    ส่งอีเมลตามรายการ   |  ซุปเปอร์ฟิลเตอร์   |   ตัวกรองพิเศษ (กรองตัวหนา/ตัวเอียง/ขีดทับ...) ...
ชุดเครื่องมือ 15 อันดับแรก12 ข้อความ เครื่องมือ (เพิ่มข้อความ, ลบอักขระ, ... )   |   50 + แผนภูมิ ประเภท (แผนภูมิ Gantt, ... )   |   40+ ใช้งานได้จริง สูตร (คำนวณอายุตามวันเกิด, ... )   |   19 การแทรก เครื่องมือ (ใส่ QR Code, แทรกรูปภาพจากเส้นทาง, ... )   |   12 การแปลง เครื่องมือ (ตัวเลขเป็นคำ, การแปลงสกุลเงิน, ... )   |   7 ผสานและแยก เครื่องมือ (แถวรวมขั้นสูง, แยกเซลล์, ... )   |   ... และอื่น ๆ

เพิ่มพูนทักษะ Excel ของคุณด้วย Kutools สำหรับ Excel และสัมผัสประสิทธิภาพอย่างที่ไม่เคยมีมาก่อน Kutools สำหรับ Excel เสนอคุณสมบัติขั้นสูงมากกว่า 300 รายการเพื่อเพิ่มประสิทธิภาพและประหยัดเวลา  คลิกที่นี่เพื่อรับคุณสมบัติที่คุณต้องการมากที่สุด...

รายละเอียด


แท็บ Office นำอินเทอร์เฟซแบบแท็บมาที่ Office และทำให้งานของคุณง่ายขึ้นมาก

  • เปิดใช้งานการแก้ไขและอ่านแบบแท็บใน Word, Excel, PowerPoint, ผู้จัดพิมพ์, Access, Visio และโครงการ
  • เปิดและสร้างเอกสารหลายรายการในแท็บใหม่ของหน้าต่างเดียวกันแทนที่จะเป็นในหน้าต่างใหม่
  • เพิ่มประสิทธิภาพการทำงานของคุณ 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