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

วิธีจัดเรียงข้อมูลคอลัมน์โดยคลิกที่ส่วนหัวใน Excel

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

doc จัดเรียงโดยคลิก 1

จัดเรียงข้อมูลโดยคลิกที่ส่วนหัวของคอลัมน์ด้วยรหัส VBA


ลูกศรสีฟ้าฟองขวา จัดเรียงข้อมูลโดยคลิกที่ส่วนหัวของคอลัมน์ด้วยรหัส VBA

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

1. คลิกขวาที่แท็บแผ่นงานที่คุณต้องการจัดเรียงข้อมูลโดยคลิกที่เซลล์แล้วเลือก ดูรหัส จากเมนูบริบทและในเปิด Microsoft Visual Basic สำหรับแอปพลิเคชัน หน้าต่างคัดลอกและวางรหัสต่อไปนี้ลงในโมดูลเปล่า:

รหัส VBA: จัดเรียงข้อมูลโดยคลิกที่ส่วนหัวของเซลล์หรือคอลัมน์:

Public blnToggle As Boolean
Private Sub Worksheet_BeforeDoubleClick _
(ByVal Target As Range, Cancel As Boolean)
'Updateby Extendoffice
Dim LastColumn As Long, keyColumn As Long, LastRow As Long
Dim SortRange As Range
LastColumn = _
Cells.Find(What:="*", After:=Range("A1"), _
SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
keyColumn = Target.Column
If keyColumn > LastColumn Then Exit Sub
Application.ScreenUpdating = False
Cancel = True
LastRow = Cells(Rows.Count, keyColumn).End(xlUp).Row
Set SortRange = Target.CurrentRegion
blnToggle = Not blnToggle
If blnToggle = True Then
SortRange.Sort _
Key1:=Cells(2, keyColumn), Order1:=xlAscending, Header:=xlYes
Else
SortRange.Sort _
Key1:=Cells(2, keyColumn), Order1:=xlDescending, Header:=xlYes
End If
Set SortRange = Nothing
Application.ScreenUpdating = True
End Sub

doc จัดเรียงโดยคลิก 2

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


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

วิธีการเปลี่ยนค่าเซลล์โดยคลิกที่เซลล์?

วิธีกรองข้อมูลเพียงแค่คลิกเนื้อหาเซลล์ใน 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 (9)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Hallo,
der Code funktioniert auch gut bei mir. Allerdings würde ich gerne die oberen beiden Zeilen nicht mit sortieren, da diese die Überschriften sind.
Wie muss ich dann diesen Code ändern?

Vielen Dank!!
This comment was minimized by the moderator on the site
Hello friend,
Here is the VBA you need:

Public blnToggle As Boolean
Private Sub Worksheet_BeforeDoubleClick _
(ByVal Target As Range, Cancel As Boolean)
'Updateby Extendoffice
Dim LastColumn As Long, keyColumn As Long, LastRow As Long
Dim SortRange As Range
LastColumn = _
Cells.Find(What:="*", After:=Range("A1"), _
SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
keyColumn = Target.Column
If keyColumn > LastColumn Then Exit Sub
Application.ScreenUpdating = False
Cancel = True
LastRow = Cells(Rows.Count, keyColumn).End(xlUp).Row
On Error Resume Next
Set SortRange = Target.CurrentRegion
Dim i As Long
i = 2
Set SortRange = SortRange.Offset(i, 0)
Set SortRange = SortRange.Resize(SortRange.Rows.Count - i, SortRange.Columns.Count)
blnToggle = Not blnToggle
If blnToggle = True Then
SortRange.Sort _
Key1:=Cells(2, keyColumn), Order1:=xlAscending, Header:=xlNo
Else
SortRange.Sort _
Key1:=Cells(2, keyColumn), Order1:=xlDescending, Header:=xlNo
End If
Set SortRange = Nothing
Application.ScreenUpdating = True
End Sub


If you have headers of 3 rows, just change "i =2" to "i =3" in the VBA. Hope it helps. Have a great day.

Sincerely,
Mandy
This comment was minimized by the moderator on the site
Hi Mandy/all,

Is it possible to change your code to only sort when the headers are double clicked instead of any cell?

Thank you very much!
This comment was minimized by the moderator on the site
Hello,
To solve your problem, please apply the below code:
Public blnToggle As Boolean
Private Sub Worksheet_BeforeDoubleClick _
(ByVal Target As Range, Cancel As Boolean)
'Updateby Extendoffice
Dim LastColumn As Long, keyColumn As Long, LastRow As Long
Dim SortRange As Range
Dim xAddress As String
Dim xRgI As Range
xAddress = "A1:E2" 'The headers
Set xRgI = Intersect(Range(xAddress), Target)
If xRgI Is Nothing Then Exit Sub
LastColumn = _
Cells.Find(What:="*", After:=Range("A1"), _
SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
keyColumn = Target.Column
If keyColumn > LastColumn Then Exit Sub
Application.ScreenUpdating = False
Cancel = True
LastRow = Cells(Rows.Count, keyColumn).End(xlUp).Row
On Error Resume Next
Set SortRange = Target.CurrentRegion
Dim i As Long
i = 2
Set SortRange = SortRange.Offset(i, 0)
Set SortRange = SortRange.Resize(SortRange.Rows.Count - i, SortRange.Columns.Count)
blnToggle = Not blnToggle
If blnToggle = True Then
SortRange.Sort _
Key1:=Cells(2, keyColumn), Order1:=xlAscending, Header:=xlNo
Else
SortRange.Sort _
Key1:=Cells(2, keyColumn), Order1:=xlDescending, Header:=xlNo
End If
Set SortRange = Nothing
Application.ScreenUpdating = True
End Sub



Please have a try, hope it can help you!
This comment was minimized by the moderator on the site
This works perfectly! Thank you very much skyyang!
This comment was minimized by the moderator on the site
No can do crackerjack - don't work
This comment was minimized by the moderator on the site
Hi, Rob, The above code works well in my Excel, can you give your problem a screenshot here?
This comment was minimized by the moderator on the site
Doesn't work, nothing happens, know how to create module in vba, did that, saved and nothing when header double clicked. Please fix it.
This comment was minimized by the moderator on the site
Works ok to ascend, double click a 2nd time as stated to descend does nothing
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations