:D snasui.com ยินดีต้อนรับ :D
ยินดีต้อนรับสู่กระดานถามตอบ Excel and VBA และอื่น ๆ ที่เป็นมิตรกับทุกท่าน มีไฟล์แนบมหาศาล ช่วยให้ท่านค้นหาและติดตามศึกษาได้โดยง่าย :thup: สมาชิกท่านใดที่ยังไม่ได้ระบุ Version ของ Excel ที่ใช้งานจริง สามารถทำตาม Link นี้เพื่อจะได้รับคำตอบที่ตรงกับ Version ของท่านครับ :arrow: ระบุ Version ของ Excel
:!: โปรดทราบ :!:
  1. กรุณาอ่านกฎการใช้บอร์ด (Forum rules) ในตำแหน่งด้านบนของแต่ละบอร์ด
  2. การสมัครสมาชิก การ Login การกู้คืนรหัสผ่าน
    1. สมัครสมาชิกดูขั้นตอนได้ที่ :arrow: สมัครสมาชิก
    2. Login เข้าระบบโดยคลิกปุ่ม Login ตรงมุมขวาบนของหน้านี้ :roll:
    3. การ Login ผ่าน Facebook ดูวิธีที่ :arrow: Login ผ่าน Facebook
    4. ลืมรหัสผ่านสามารถรับรหัสใหม่ได้ที่ :arrow: Reset รหัสผ่าน
  3. มีปัญหาการใช้งาน แจ้งผู้ดูแลระบบได้ที่ :arrow: ติดต่อผู้ดูแลระบบ
  4. กำหนดการตั้งค่าส่วนตัว เช่นตั้งค่าภาษาเป็นไทยหรืออังกฤษดูได้ที่ :arrow: ตั้งค่าส่วนตัว
  5. การตั้งและตอบกระทู้ดูได้ที่ :arrow: วิธีการตั้งและตอบกระทู้
  6. การจัดรูปแบบตัวอักษรด้วย bbcode ในช่องแสดงความคิดเห็นดูได้ที่ :arrow: จัดรูปแบบตัวอักษร
  7. กำหนดขนาดตัวอักษรใน Browser ดูได้ที่ :arrow: กำหนดขนาดตัวอักษรใน Browser

ทำให้ ComboBox filter ข้อมูลหลาย criteria

ฟอรัมถาม-ตอบปัญหาการใช้งานสูตรและฟังก์ชัน Excel
Forum rules
  1. ไม่อนุญาตให้ใช้ภาษาแชทในการถามและตอบปัญหา ไม่ใช้คำว่า "คับ" หรือ "อ่ะครับ" แทนคำว่า "ครับ" ไม่ใช้คำว่า "เด๋ว" แทนคำว่า "เดี๋ยว" เป็นต้น เนื่องจากเมื่อแปลเป็นภาษาต่างประเทศแล้วจะให้ความหมายผิดไปจากที่ควรจะเป็น
  2. ห้ามถามโดยระบุชื่อผู้ตอบและต้องตั้งชื่อกระทู้ให้สื่อถึงปัญหาที่จะถาม ไม่ตั้งชื่อว่า ช่วยด้วยครับ, มีปัญหามาปรึกษาครับ เป็นต้น
  3. กรุณาอธิบายปัญหาและระบุคำตอบที่ต้องการมาในกระทู้ด้วยเสมอถึงแม้จะอธิบายไว้ในไฟล์แนบแล้วก็ตาม ทั้งนี้เพื่ออำนวยความสะดวกแก่เพื่อนสมาชิกในการค้นหาข้อมูล
  4. กรุณาแนบไฟล์ตัวอย่างพร้อมแสดงคำตอบที่ถูกต้องมาในไฟล์ด้วยเพื่อให้ง่ายต่อการทำความเข้าใจและสะดวกต่อการตอบคำถาม (ขนาดไฟล์ไม่เกิน 500Kb ขนาดภาพไม่เกิน 800*600 Pixel) ไม่แนบเป็น Link มาจากแหล่งอื่นที่อาจจะถูกลบทิ้งไปโดยต้นทางในภายหลัง นอกจากนี้ไม่ควรแนบไฟล์ที่มีข้อมูลสำคัญอันก่อให้เกิดความเสียหายกับตนเองและผู้อื่น
  5. กรณีเป็นคำถามเกี่ยวกับ Programming เช่น VBA, VB.Net, C#, SQL ฯลฯ ต้องลองเขียนมาเองก่อนเสมอ ถามเฉพาะที่ติดปัญหา ระบุ Module, Procedure ที่ติดปัญหาให้ชัดเจน กรุณาโพสต์ Code ให้แสดงเป็น Code คือเปิดด้วย [code] และปิดด้วย [/code] ตัวอย่างเช่น [code]dim r as range[/code] เพื่อให้แตกต่างจากข้อความทั่วไป สะดวกในการอ่านและทดสอบ (คลิกเพื่อดูตัวอย่าง)
  6. กรุณาแจ้งผลการใช้งานเมื่อได้รับคำตอบว่าตรงกับความต้องการหรือไม่
warotthan
Member
Member
Posts: 5
Joined: Sat Aug 03, 2019 3:26 pm

ทำให้ ComboBox filter ข้อมูลหลาย criteria

#1

Post by warotthan »

สวัสดีครับ

รบกวน สอบถาม Logic หรือ เขียน code vba ในไฟล์ที่ Attached ครับ

ต้องการทำให้ dropdown ใน ComboBox โชว์ข้อมูลที่ Filter
เช่น เลือก ComboBox Sex เป็น Male => ComboBox Country ก็จะ Filter เหลือแค่ (TH,MA,VN)
และสามารถเลือกกลับข้างกันได้
Concept คล้ายกับการ Filter แต่ให้มาโชว์ใน Userform ครับ

รบกวนด้วยครับ
You do not have the required permissions to view the files attached to this post.
User avatar
snasui
Site Admin
Site Admin
Posts: 31256
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:

Re: ทำให้ ComboBox filter ข้อมูลหลาย criteria

#2

Post by snasui »

:D การใช้งาน VBA ต้องเขียน Code มาเองก่อนตามกฎการใช้บอร์ดข้อ 5 ด้านบน ติดตรงไหนค่อยถามกันต่อครับ

ลองศึกษาเกี่ยวกับการ Add item ให้กับ ComboBox ตาม Link นี้ครับ Add Item
warotthan
Member
Member
Posts: 5
Joined: Sat Aug 03, 2019 3:26 pm

Re: ทำให้ ComboBox filter ข้อมูลหลาย criteria

#3

Post by warotthan »

ผมลองเขียนให้มัน filter ได้แล้ว แต่มันได้แค่ criteria เดียว จะต้องทำอย่างไรเพื่อให้มันสามารถ filter ได้หลายๆ criteria ครับ

Code: Select all

Private Sub ComboBox1_Change()

ComboBox2.Clear
ComboBox3.Clear

Dim LastRow As Long
LastRow = Cells(Rows.Count, 1).End(xlUp).Row

For i = 2 To LastRow
    y = Application.WorksheetFunction.CountIfs(Range(Cells(2, 3), Cells(i, 3)), Cells(i, 3), Range(Cells(2, 2), Cells(i, 2)), ComboBox1)
    z = Application.WorksheetFunction.CountIfs(Range(Cells(2, 4), Cells(i, 4)), Cells(i, 4), Range(Cells(2, 2), Cells(i, 2)), ComboBox1)

If Cells(i, 2) = ComboBox1 Then
    If y = 1 Then ComboBox2.AddItem Cells(i, 3)
    If z = 1 Then ComboBox3.AddItem Cells(i, 4)
End If

Next i

End Sub

Private Sub ComboBox2_Change()

ComboBox1.Clear
ComboBox3.Clear

Dim LastRow As Long
LastRow = Cells(Rows.Count, 1).End(xlUp).Row

For i = 2 To LastRow
    x = Application.WorksheetFunction.CountIfs(Range(Cells(2, 2), Cells(i, 2)), Cells(i, 2), Range(Cells(2, 3), Cells(i, 3)), ComboBox2)
    z = Application.WorksheetFunction.CountIfs(Range(Cells(2, 4), Cells(i, 4)), Cells(i, 4), Range(Cells(2, 3), Cells(i, 3)), ComboBox2)

If Cells(i, 3) = ComboBox2 Then
    If x = 1 Then ComboBox1.AddItem Cells(i, 2)
    If z = 1 Then ComboBox3.AddItem Cells(i, 4)
End If

Next i

End Sub

Private Sub ComboBox3_Change()

ComboBox1.Clear
ComboBox2.Clear

Dim LastRow As Long
LastRow = Cells(Rows.Count, 1).End(xlUp).Row

For i = 2 To LastRow
    x = Application.WorksheetFunction.CountIfs(Range(Cells(2, 2), Cells(i, 2)), Cells(i, 2), Range(Cells(2, 4), Cells(i, 4)), ComboBox3)
    y = Application.WorksheetFunction.CountIfs(Range(Cells(2, 3), Cells(i, 3)), Cells(i, 3), Range(Cells(2, 4), Cells(i, 4)), ComboBox3)

If Cells(i, 4) = ComboBox3 Then
    If x = 1 Then ComboBox1.AddItem Cells(i, 2)
    If y = 1 Then ComboBox2.AddItem Cells(i, 3)
End If

Next i
End Sub

Private Sub UserForm_Initialize()

Dim LastRow As Long
LastRow = Cells(Rows.Count, 1).End(xlUp).Row

Dim x As Integer, y As Integer, z As Integer

For i = 2 To LastRow
    x = Application.WorksheetFunction.CountIf(Range(Cells(2, 2), Cells(i, 2)), Cells(i, 2))
    y = Application.WorksheetFunction.CountIf(Range(Cells(2, 3), Cells(i, 3)), Cells(i, 3))
    z = Application.WorksheetFunction.CountIf(Range(Cells(2, 4), Cells(i, 4)), Cells(i, 4))
    
    If x = 1 Then ComboBox1.AddItem Cells(i, 2)
    If y = 1 Then ComboBox2.AddItem Cells(i, 3)
    If z = 1 Then ComboBox3.AddItem Cells(i, 4)
Next i
    
End Sub
ไฟล์ที่มี Code ตามที่ Attached ครับ

รบกวนด้วยครับ ขอบคุณมากๆครับ
You do not have the required permissions to view the files attached to this post.
User avatar
snasui
Site Admin
Site Admin
Posts: 31256
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:

Re: ทำให้ ComboBox filter ข้อมูลหลาย criteria

#4

Post by snasui »

:D ตัวอย่าง Code ครับ

Code: Select all

Dim sList As Object
Dim cLsList As Object
Dim cnList As Object
Dim r As Range, rall As Range

Private Sub ComboBox1_Change()
    Set cLsList = CreateObject("Scripting.Dictionary")
    ComboBox2.Clear
    With Sheets("Sheet1")
        For Each r In rall.Offset(0, 1)
            If r.Offset(0, -1).Value = Me.ComboBox1.Value _
                And Not cLsList.Exists(r.Value) Then
                cLsList.Add Key:=r.Value, Item:=r.Value
                Me.ComboBox2.AddItem r.Value
            End If
        Next r
    End With
End Sub

Private Sub ComboBox2_Change()
    Set cnList = CreateObject("Scripting.Dictionary")
    ComboBox3.Clear
    With Sheets("Sheet1")
        For Each r In rall.Offset(0, 2)
            If r.Offset(0, -2).Value = Me.ComboBox1.Value _
                And r.Offset(0, -1).Value = Me.ComboBox2.Value _
                And Not cnList.Exists(r.Value) Then
                cnList.Add Key:=r.Value, Item:=r.Value
                Me.ComboBox3.AddItem r.Value
            End If
        Next r
    End With
End Sub

Private Sub UserForm_Initialize()
    Set sList = CreateObject("Scripting.Dictionary")
    With Sheets("Sheet1")
        Set rall = .Range("b2", .Range("b" & .Rows.Count).End(xlUp))
        For Each r In rall
            If Not sList.Exists(r.Value) Then
                sList.Add Key:=r.Value, Item:=r.Value
                Me.ComboBox1.AddItem r.Value
            End If
        Next r
    End With
End Sub
warotthan
Member
Member
Posts: 5
Joined: Sat Aug 03, 2019 3:26 pm

Re: ทำให้ ComboBox filter ข้อมูลหลาย criteria

#5

Post by warotthan »

ขอบคุณมากครับ จากตัวอย่าง Code มันทำการ Filter เรียงจาก ComboBox1 ไป 2 และ ไป3
ถ้าต้องการให้สามารถ Filter ข้ามกัน และสามารถสลับกันได้ เช่น ComboBox2 ไป3 และ ไป1 ต้องทำอย่างไรครับ
User avatar
snasui
Site Admin
Site Admin
Posts: 31256
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:

Re: ทำให้ ComboBox filter ข้อมูลหลาย criteria

#6

Post by snasui »

:D ต้องปรับปรุงมาเอง ติดตรงไนค่อยถามกันต่อ

ทุกคำถามจะต้องผ่านกระบวนการปรับปรุงมาเองก่อนแล้ว ไม่สามารถนำคำตอบมาถามต่อเนื่องได้โดยที่ยังไม่มีการปรับปรุงมาด้วยตนเองก่อนครับ
warotthan
Member
Member
Posts: 5
Joined: Sat Aug 03, 2019 3:26 pm

Re: ทำให้ ComboBox filter ข้อมูลหลาย criteria

#7

Post by warotthan »

รบกวนถาม logic ในการเขียนได้มั้ยครับ เพราะผมนึกไม่ออกจริงๆ แล้วเดี๋ยวนำไปปรับปรุงเองครับ :D
User avatar
snasui
Site Admin
Site Admin
Posts: 31256
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:

Re: ทำให้ ComboBox filter ข้อมูลหลาย criteria

#8

Post by snasui »

:D หลักการคือหาค่าไม่ซ้ำแล้วนำไป Add ไว้ใน ComboBox ที่เกี่ยวข้องครับ

การหาค่าไม่ซ้ำใช้ Scripting.Dictionay มาช่วยครับ
warotthan
Member
Member
Posts: 5
Joined: Sat Aug 03, 2019 3:26 pm

Re: ทำให้ ComboBox filter ข้อมูลหลาย criteria

#9

Post by warotthan »

พยายามให้สามารเลือก filter จาก ComboBox ไหนก่อนก็ได้ โดย ComboBox ที่เลือกแล้วจะ Lock ไว้ แต่ยัง Run ไม่ผ่านครับ
รบกวนอาจารย์ช่วยด้วยครับ

Code: Select all


Dim sList As Object
Dim cLsList As Object
Dim cnList As Object
Dim r As Range, rall As Range

Private Sub ComboBox1_Change()
    Set cLsList = CreateObject("Scripting.Dictionary")

    With Sheets("Sheet1")
    
    If ComboBox2 = "" Then
        ComboBox2.Clear
        For Each r In rall.Offset(0, 1)
            If r.Offset(0, -1).Value = Me.ComboBox1.Value _
                And Not cLsList.Exists(r.Value) Then
                cLsList.Add Key:=r.Value, Item:=r.Value
                Me.ComboBox2.AddItem r.Value
            End If
    ComboBox1.Enabled = False
        Next r
    End If
    
      If ComboBox3 = "" Then
        ComboBox3.Clear
        For Each r In rall.Offset(0, 2)
            If r.Offset(0, -2).Value = Me.ComboBox1.Value _
                And Not cLsList.Exists(r.Value) Then
                cLsList.Add Key:=r.Value, Item:=r.Value
                Me.ComboBox3.AddItem r.Value
            End If
                ComboBox1.Enabled = False
        Next r
          End If
          
    End With
    
End Sub

Private Sub ComboBox2_Change()
    Set cnList = CreateObject("Scripting.Dictionary")

    With Sheets("Sheet1")

        If ComboBox1 = "" Then
        ComboBox1.Clear
        For Each r In rall.Offset(0, 0)
            If r.Offset(0, 1).Value = Me.ComboBox2.Value _
                And Not cLsList.Exists(r.Value) Then
                cLsList.Add Key:=r.Value, Item:=r.Value
                Me.ComboBox1.AddItem r.Value
            End If
    ComboBox2.Enabled = False
        Next r
    End If
    
      If ComboBox3 = "" Then
        ComboBox3.Clear
        For Each r In rall.Offset(0, 2)
            If r.Offset(0, -1).Value = Me.ComboBox2.Value _
                And Not cLsList.Exists(r.Value) Then
                cLsList.Add Key:=r.Value, Item:=r.Value
                Me.ComboBox3.AddItem r.Value
            End If
                ComboBox2.Enabled = False
        Next r
          End If
          
    End With
End Sub

Private Sub ComboBox3_Change()
    Set cnList = CreateObject("Scripting.Dictionary")

    With Sheets("Sheet1")

        If ComboBox1 = "" Then
        ComboBox1.Clear
        For Each r In rall.Offset(0, 0)
            If r.Offset(0, 2).Value = Me.ComboBox3.Value _
                And Not cLsList.Exists(r.Value) Then
                cLsList.Add Key:=r.Value, Item:=r.Value
                Me.ComboBox1.AddItem r.Value
            End If
    ComboBox3.Enabled = False
        Next r
    End If
    
      If ComboBox2 = "" Then
        ComboBox2.Clear
        For Each r In rall.Offset(0, 1)
            If r.Offset(0, 1).Value = Me.ComboBox3.Value _
                And Not cLsList.Exists(r.Value) Then
                cLsList.Add Key:=r.Value, Item:=r.Value
                Me.ComboBox2.AddItem r.Value
            End If
                ComboBox3.Enabled = False
        Next r
          End If
          
    End With
    
End Sub

Private Sub UserForm_Initialize()
    Set sList = CreateObject("Scripting.Dictionary")
    With Sheets("Sheet1")
    
        Set rall = .Range("b2", .Range("b" & .Rows.Count).End(xlUp))
        For Each r In rall
            If Not sList.Exists(r.Value) Then
                sList.Add Key:=r.Value, Item:=r.Value
                Me.ComboBox1.AddItem r.Value
            End If
        Next r
        
        Set rbll = .Range("c2", .Range("c" & .Rows.Count).End(xlUp))
        For Each r In rbll
            If Not sList.Exists(r.Value) Then
                sList.Add Key:=r.Value, Item:=r.Value
                Me.ComboBox2.AddItem r.Value
            End If
        Next r
        
          Set rcll = .Range("d2", .Range("d" & .Rows.Count).End(xlUp))
          For Each r In rcll
            If Not sList.Exists(r.Value) Then
                sList.Add Key:=r.Value, Item:=r.Value
                Me.ComboBox3.AddItem r.Value
            End If
        Next r
        
    End With
End Sub


User avatar
snasui
Site Admin
Site Admin
Posts: 31256
Joined: Sun Jan 24, 2010 12:33 pm
Location: Songkhla, Thailand
Excel Ver: 2010, 2019
Contact:

Re: ทำให้ ComboBox filter ข้อมูลหลาย criteria

#10

Post by snasui »

:D ช่วยยกตัวอย่างการเลือกมาทั้ง 3 ComboBox ว่า

หากเลือก ComboBox1, 2 และ 3 เป็นค่าหนึ่ง ๆ แล้วให้ ComboBox ที่เหลือแสดงแสดงอย่างไร จะได้สะดวกในการทำความเข้าใจครับ
Post Reply