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

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

ตัวอย่างเช่น คุณมีตารางการซื้อตามที่แสดงภาพหน้าจอด้านล่าง ตอนนี้คุณต้องการให้คอลัมน์ราคาจัดเรียงโดยอัตโนมัติเมื่อคุณป้อนตัวเลข/ราคาใหม่ในคอลัมน์นี้ คุณจะแก้ไขได้อย่างไร ที่นี่ฉันแนะนำมาโคร VBA เพื่อช่วยให้คุณจัดเรียงคอลัมน์เฉพาะตามค่าใน Excel โดยอัตโนมัติ

เรียงคอลัมน์อัตโนมัติตามค่าด้วย VBA


เรียงคอลัมน์อัตโนมัติตามค่าด้วย VBA

แมโคร VBA นี้จะจัดเรียงข้อมูลทั้งหมดในคอลัมน์เฉพาะโดยอัตโนมัติทันทีที่คุณป้อนข้อมูลใหม่หรือเปลี่ยนค่าในคอลัมน์ใน Excel

1. คลิกขวาที่ชื่อแผ่นงานปัจจุบันในไฟล์ แถบแท็บแผ่นงานแลวคลิกปุ the ม ดูรหัส จากเมนูคลิกขวา

2. ในกล่องโต้ตอบ Microsoft Visual Basic for Application ที่เปิดขึ้นให้วางรหัสแมโคร VBA ต่อไปนี้ลงในหน้าต่างเปิด

VBA: เรียงคอลัมน์อัตโนมัติใน Excel

Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Not Intersect(Target, Range("B:B")) Is Nothing Then
Range("B1").Sort Key1:=Range("B2"), _
Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom
End If
End Sub

หมายเหตุ:
1) ในรหัส vba ด้านบน BB หมายความว่าจะจัดเรียงคอลัมน์ B โดยอัตโนมัติ B1 เป็นเซลล์แรกในคอลัมน์ B B2 เป็นเซลล์ที่สองในคอลัมน์ B และคุณสามารถเปลี่ยนได้ตามความต้องการของคุณ
2) ข้อมูลโค้ด Header:=xlใช่ ในแถวที่ 5 จะบอก Excel ว่าช่วงที่คุณจะเรียงลำดับมีส่วนหัว เพื่อไม่ให้รวมแถวแรกของช่วงเมื่อเรียงลำดับ หากไม่มีส่วนหัว โปรดเปลี่ยนเป็น ส่วนหัว:=xlNo; และเปลี่ยน Key1:=Range("B2") ในแถวที่ 4 ถึง Key1:=Range("B1").

3. จากนั้นกลับไปที่แผ่นงานเมื่อคุณป้อนตัวเลขใหม่ในคอลัมน์ราคาหรือแก้ไขราคาที่มีอยู่คอลัมน์ราคาจะเรียงลำดับจากน้อยไปมากโดยอัตโนมัติ

หมายเหตุ: เมื่อคุณป้อนตัวเลขใหม่ในคอลัมน์ราคา คุณต้องป้อนตัวเลขในเซลล์ว่างเซลล์แรกด้านล่างตัวเลขเดิม หากมีเซลล์ว่างระหว่างตัวเลขที่ป้อนใหม่และตัวเลขเดิม รวมทั้งเซลล์ว่างระหว่างตัวเลขเดิม คอลัมน์นี้จะไม่จัดเรียงโดยอัตโนมัติ


สาธิต: จัดเรียงคอลัมน์อัตโนมัติตามค่าด้วย VBA ใน Excel


Kutools สำหรับ Excel: เครื่องมือแสนสะดวกกว่า 300 รายการอยู่แค่เพียงปลายนิ้วสัมผัส! เริ่มทดลองใช้ฟรี 30 วันโดยไม่มีข้อจำกัดด้านฟีเจอร์ตั้งแต่วันนี้ Download Now!

จัดเรียงตามความถี่ของการเกิดขึ้นใน Excel ได้อย่างง่ายดาย

Kutools สำหรับ Excel's การเรียงลำดับขั้นสูง ยูทิลิตี้รองรับการจัดเรียงข้อมูลตามความยาวข้อความนามสกุลค่าสัมบูรณ์ความถี่ ฯลฯ ใน Excel ได้อย่างรวดเร็ว


จัดเรียงโฆษณาตามความถี่ 2

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

🤖 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 (37)
Rated 5 out of 5 · 1 ratings
This comment was minimized by the moderator on the site
such a good information thanks a lot
This comment was minimized by the moderator on the site
Maravilhoso! Muito obrigada pela informação, amigo!
Rated 5 out of 5
This comment was minimized by the moderator on the site
I love this, but I'd like to know what I should do if I want it to be multiple different ranges in the same column. When I use the code above, it works for the first table that I have but the bottom two tables don't sort automatically. I tried changing the range, I also duplicated the code and changed the code to match the tables, but nothing is working.

For example:
Range("L8").Sort Key1:=Range("L37"),Range("L41").Sort Key1:=Range("L62") _

Or just duplicating the code like this:
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Not Intersect(Target, Range("L:L")) Is Nothing Then
Range("L8").Sort Key1:=Range("L37"), _
Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom
End If
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Not Intersect(Target, Range("L:L")) Is Nothing Then
Range("L41").Sort Key1:=Range("L62"), _
Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom
End If
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Not Intersect(Target, Range("L:L")) Is Nothing Then
Range("L66").Sort Key1:=Range("L100"), _
Order1:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom

But then it will tell me that "Ambiguos name detected: Worksheet_Change" but it won't do that if I only have the code one. Would anyone be able to help me out?
Thank you!
This comment was minimized by the moderator on the site
Hi there,

You can use the vba below:
Private Sub Worksheet_Change(ByVal Target As Range)
    On Error Resume Next
    If Not Intersect(Target, Range("K:K")) Is Nothing Then
        Range("K32:K36").Sort Key1:=Range("K32"), _
        Order1:=xlAscending, Header:=xlNo, _
        OrderCustom:=1, MatchCase:=False, _
        Orientation:=xlTopToBottom
        
        Range("K38:K42").Sort Key1:=Range("K38"), _
        Order1:=xlAscending, Header:=xlNo, _
        OrderCustom:=1, MatchCase:=False, _
        Orientation:=xlTopToBottom        

        Range("K44:K46").Sort Key1:=Range("K44"), _
        Order1:=xlAscending, Header:=xlNo, _
        OrderCustom:=1, MatchCase:=False, _
        Orientation:=xlTopToBottom
    End If
End Sub


Amanda
This comment was minimized by the moderator on the site
01. I have sorted on Name data in Excel Worksheet as Sheet1.
02. I want the Names that are repeated in New Worksheet as Sheet2.
This comment was minimized by the moderator on the site
Hallo
Ich habe folgenden Code, aber die Sortierung klappt leider nicht.
evtl nur eine kleine Anpassung, aber ich verzweifle hier seit Tagen.
Danke im voraus.


Sub Eindeutige_Daten()

Dim rng As Range
Dim InputRng As Range, OutRng As Range

Set dt = CreateObject("Scripting.Dictionary")

xTitleId = "Eindeutige Daten"

Set InputRng = Application.Selection
Set InputRng = Application.InputBox("Range :", xTitleId, InputRng.Address, Type:=8)
Set OutRng = Application.InputBox("Out put to (single cell):", xTitleId, Type:=8)

For Each rng In InputRng
If rng.Value <> "" Then
dt(rng.Value) = ""
End If
Next

OutRng.Range("A1").Resize(dt.Count) = Application.WorksheetFunction.Transpose(dt.Keys)

'** Zelladresse in Spalten- und Zeilenangabe trennen
Dim wert() As String
wert = Split(OutRng.Address, "$")

letztezeile = ActiveSheet.Cells(1048576, wert(1)).End(xlUp).Row

With ActiveWorksheet.Sort
.SetRange Range(OutRng & ":" & wert(1) & letztezeile)
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

Calculate

End Sub
This comment was minimized by the moderator on the site
Hi there,

Please debug the below snipt of your code and see if there is a problem.
With ActiveWorksheet.Sort
.SetRange Range(OutRng & ":" & wert(1) & letztezeile)
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

Remember to check the values you entered.
If there is still questions, please don't hesitate to ask me.

Amanda
This comment was minimized by the moderator on the site
Love it. Works for me.

But when I repeat the exact same steps at another Excel file, and enter a number in the colomn for it to autosort, Excel closes.
This comment was minimized by the moderator on the site
Hi,
This is extremely useful.
Is there a way to expand the formula to cover multiple columns? For example, to sort data based on values on first, column B, and then column C?I would really appreciate any solutions!
This comment was minimized by the moderator on the site
Amazing thanks!!!
This comment was minimized by the moderator on the site
So this seems to work when the data is manually entered but doesn't work when it is a table that repopulates from another file....is there any way to do that?
This comment was minimized by the moderator on the site
Hi, it arrange the entire row but I have some link on cells into specific folder which is left behind after sort.
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