使用复选框过滤多个条件

时间:2017-07-20 14:16:01

标签: excel vba excel-vba

我有一张名为Result的表单,我已经使用Listbox创建了一个用户表单。

我想过滤并查看特定位置的结果。我有7个位置。如果我也点击了多个位置,我应该能够看到结果。

为此,我创建了一个带有Checkbox的Listbox,并尝试了如下代码,

Private Sub Filter1()

Dim Ws As Worksheet
Dim strCriteria() As String
Dim arrIdx As Integer

Dim cBox As Control

arrIdx = 0
For Each cBox In Me.Controls
    If TypeName(cBox) = "Listbox" Then
        If cBox.Value = True Then
            ReDim Preserve strCriteria(0 To arrIdx)
            strCriteria(arrIdx) = cBox.Caption
            arrIdx = arrIdx + 1
        End If
    End If
Next cBox

Set Ws = ThisWorkbook.Sheets("Result")
If arrIdx = 0 Then
    Ws.UsedRange.AutoFilter
Else
    Ws.Range("A:R").AutoFilter Field:=12, Criteria1:=Array(strCriteria), Operator:=xlFilterValues
End If

End Sub

我在listbox_change中调用了这个过滤器函数。我发现没有任何过滤功能。任何人都可以提供帮助,我错了,需要修改什么 -

I have an useform like this, when i click on both the Location, I should be able to see the result. If i click on any one also I should b able to see the filter in my sheet accordingly. I used Listbox1.additem to add the Location in my listbox.

1 个答案:

答案 0 :(得分:0)

strCriteria 已经是一个数组

尝试Ws.Range("A:R").AutoFilter Field:=12, Criteria1:=strCriteria, Operator:=xlFilterValues