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

วิธีกรองแถวอัตโนมัติตามค่าเซลล์ใน Excel

ผู้เขียน: Xiaoyang แก้ไขล่าสุด: 2020-04-26

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

แถวกรองอัตโนมัติตามค่าเซลล์ที่คุณป้อนด้วยรหัส VBA

กรองข้อมูลตามเกณฑ์หลายข้อหรือเงื่อนไขเฉพาะอื่น ๆ เช่นตามความยาวของข้อความโดยคำนึงถึงตัวพิมพ์เล็กและใหญ่


แถวกรองอัตโนมัติตามค่าเซลล์ที่คุณป้อนด้วยรหัส VBA

สมมติว่าฉันมีช่วงข้อมูลต่อไปนี้เมื่อฉันป้อนเกณฑ์ในเซลล์ E1 และ E2 ฉันต้องการให้ข้อมูลถูกกรองโดยอัตโนมัติตามภาพด้านล่างที่แสดง:

ตัวกรองอัตโนมัติ doc 1

1. ไปที่แผ่นงานที่คุณต้องการกรองวันที่โดยอัตโนมัติตามค่าเซลล์ที่คุณป้อน

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

รหัส VBA: กรองข้อมูลอัตโนมัติตามค่าเซลล์ที่ป้อน:

Private Sub Worksheet_Change(ByVal Target As Range)
'Updateby Extendoffice 20160606
   If Target.Address = Range("E2").Address Then
       Range("A1:C20").CurrentRegion.AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Range("E1:E2")
   End If
End Sub

ตัวกรองอัตโนมัติ doc 2

หมายเหตุ: ในโค้ดด้านบน A1: C20 คือช่วงข้อมูลที่คุณต้องการกรอง E2 คือค่าเป้าหมายที่คุณต้องการกรองตามและ E1: E2 คือเซลล์เกณฑ์ของคุณจะถูกกรองตาม คุณสามารถเปลี่ยนได้ตามความต้องการของคุณ

3. ตอนนี้เมื่อคุณป้อนเกณฑ์ในเซลล์ E1 และ E2 และกด เข้าสู่ ข้อมูลของคุณจะถูกกรองโดยค่าของเซลล์โดยอัตโนมัติ


กรองข้อมูลตามเกณฑ์หลายข้อหรือเงื่อนไขเฉพาะอื่น ๆ เช่นตามความยาวของข้อความโดยคำนึงถึงตัวพิมพ์เล็กและใหญ่

กรองข้อมูลตามเกณฑ์หลายข้อหรือเงื่อนไขเฉพาะอื่น ๆ เช่นตามความยาวของข้อความโดยคำนึงถึงขนาดตัวพิมพ์เป็นต้น

Kutools สำหรับ Excel's ซุปเปอร์ฟิลเตอร์ คุณลักษณะเป็นยูทิลิตี้ที่มีประสิทธิภาพคุณสามารถใช้คุณสมบัตินี้เพื่อสิ้นสุดการดำเนินการต่อไปนี้:

  • กรองข้อมูลด้วยหลายเกณฑ์ กรองข้อมูลตามความยาวข้อความ
  • กรองข้อมูลตามตัวพิมพ์ใหญ่ / เล็ก กรองวันที่ตามปี / เดือน / วัน / สัปดาห์ / ไตรมาส

Kutools สำหรับ Excel: ด้วย Add-in ของ Excel ที่มีประโยชน์มากกว่า 300 รายการให้ทดลองใช้ฟรีโดยไม่มีข้อ จำกัด ใน 30 วัน ดาวน์โหลดและทดลองใช้ฟรีทันที!


สาธิต: กรองแถวอัตโนมัติตามค่าเซลล์ที่คุณป้อนด้วยรหัส VBA

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

🤖 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 (36)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
hi

thank you - this code was a life saver

but if I wish to filter when the value on E2 contains the search text.
so if i search for Grade1
it should filter
Grade1
ExampleGrade1
Grade1ETC


many thanks in advance
This comment was minimized by the moderator on the site
I scanned through the comments and didn't see a specific answer. I am looking to filter a Table that changes based on a DB query, The table range will change every time the query is refreshed, How can we account for this in the code? The function becomes pointless if I have to direct users edit the VBA code every time they refresh the table.
This comment was minimized by the moderator on the site
Good day,

I have made sheet1 with the table in the example. In sheet2 all the data are referenced to the table in sheet1.

In sheet2 I can choose the grades exactly as in the example. That works OK.

However when I change a Grade of a student in sheet1 the list is not updated in sheet2. So the filter does not work automatic.

How can this be done?
This comment was minimized by the moderator on the site
Hello, is there a way to quickly modify the VBA code to filter on values bigger than in selected cell?
This comment was minimized by the moderator on the site
Hi there,

Thank you for the great content, however I am having some issues and was wondering whether you had any ideas why.

I have adjusted my ranges so my code reads:

Sub AutoFilter()
If Target.Address = Range("E13:F14").Address Then
Worksheets('Data Archive').Range("A1:C20").CurrentRegion.AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Range("F10:F1048576")
End If
End Sub


However, when I run the macro a "Compile Error: Syntex Error" message appears.

Thank you in advance

Stan
This comment was minimized by the moderator on the site
Hello, Stan,
Do you set the data range and criteria range correctly in the code?Or you can take your data range as a screenshot here.
Thank you!
This comment was minimized by the moderator on the site
Thank you very much for this. It worked for me fine to a certain extent, please let me explain:

I have multiple tables in my workbook. When I applied the above code, it only worked for one table, but not for the other two tables. Let's say the first table is, as in your example, from A1:C20. The second table is from A22:C40. The third from A42:C60. All tables have the info about the "Grade", however they have different columns & Date and therefore cannot be consolidated into one big tables but rather 3 smaller tables.

How would the code need to like like so that if I put the criteria: "Grade1", all 3 tables will be automatically filtered for "Grade1" ?

Thank you very much in advance.

Best regards
This comment was minimized by the moderator on the site
Thank you, for this, it was really helpful. i just have one question, after selected the name in the drop down menu (i have names as criteria), it only shows people with that name, as it should, but how can i do so after i have selected a name, then i want so see all the rows of my table??

hope you can help me out.

kind regards
This comment was minimized by the moderator on the site
Hi, Peter,
This code is worked well for fitering the entire rows of the data, could you enter your table range correctlly?

A1:C20 is the range of your data, E1:E2 is the criteria range.
Range("A1:C20").CurrentRegion.AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Range("E1:E2")
This comment was minimized by the moderator on the site
Hey guys,
perfect Explanation, thank you very much.
1 Little question: if I want to filter with 2,3 4 or more criterias how do I do this?
For example I want to say I wanna see the Name Henry, with Grade 1 and this Age...so not just 1 criteria but for example 3..=?


thanks for the respond


Kind regards,


TIM
This comment was minimized by the moderator on the site
Hi, tim,
To auto filter data based on multiple criteria, you should apply the below code: (please change the cell references to your need)

Private Sub Worksheet_Change(ByVal Target As Range)
'Update by Extendoffice
Dim xVStr As String
Dim xFStr As String
xVStr = "E22:G22" 'the criteria that you want to filter based on
xFStr = "E21:G22" 'the range contains the header of the criteria
If Not (Intersect(Range(xVStr), Target) Is Nothing) Then
Range("A1:C17").CurrentRegion.AdvancedFilter Action:=xlFilterInPlace, _
CriteriaRange:=Range(xFStr)
End If
End Sub


Please try, hope it can help you!
This comment was minimized by the moderator on the site
Hello,

What if I got the filtered data in a different tab(sheet 2) in the same workbook and the cell that the filter needs to refer to is in the first tab(sheet 1). I used this VBA but is not working like that, only if I have both the criteria cell(E2 in this VBA) in the same tab with the filtered data(A1:C20)
This comment was minimized by the moderator on the site
There might be a mistake in the instructions. Instead of pasting the code into a blank Module, one should paste it into the Sheet window. For example, if the macro is to work on Sheet1, the code should be pasted into Microsoft Excel Objects -> Sheet1(Sheet1). Only then it works for me on Excel 2016.

Thanks for the code!
This comment was minimized by the moderator on the site
Hi, mjr,
There is no mistake in this article, the article said, you should put the VBA code into the sheet module by right click the sheet name and then choose View Code to go to the module.
But, your operation is correct as well.
Thank you for your comment.
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