ผมลองเขียนให้มัน 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