我想做的是使用多个复选框表单来基于“项目”工作表中的表格设置过滤器值。我已经能够成功地对个人执行此操作,但是我需要做的是采用当前33个复选框的任意组合来筛选满足所有选定条件的行。以下是我目前拥有的东西,并且它会不断地回弹。我的范围运行K:AQ列,其值等于11到43,如下所示。感谢您能提供的任何帮助。
Private Sub FilterButton_Click()
Dim wb As Workbook: Set wb = ThisWorkbook
Dim ws As Worksheet
Set ws = wb.Sheets("Projects")
Dim fld As Long
For fld = 11 To 43
Next
If frmSearch.CheckBox1 = True = True Or _
frmSearch.CheckBox2 = True Or frmSearch.CheckBox3 = True Or _
frmSearch.CheckBox4 = True Or frmSearch.CheckBox5 = True Or _
frmSearch.CheckBox6 = True Or frmSearch.CheckBox7 = True Or _
frmSearch.CheckBox8 = True Or frmSearch.CheckBox9 = True Or _
frmSearch.CheckBox10 = True Or frmSearch.CheckBox11 = True Or _
frmSearch.CheckBox12 = True Or frmSearch.CheckBox13 = True Or _
frmSearch.CheckBox14 = True Or frmSearch.CheckBox15 = True Or _
frmSearch.CheckBox16 = True Or frmSearch.CheckBox17 = True Or _
frmSearch.CheckBox18 = True Or frmSearch.CheckBox19 = True Or _
frmSearch.CheckBox20 = True Or frmSearch.CheckBox21 = True Or _
frmSearch.CheckBox22 = True Or frmSearch.CheckBox23 = True Or _
frmSearch.CheckBox24 = True Or frmSearch.CheckBox25 = True Or _
frmSearch.CheckBox26 = True Or frmSearch.CheckBox27 = True Or _
frmSearch.CheckBox28 = True Or frmSearch.CheckBox29 = True Or _
frmSearch.CheckBox30 = True Or frmSearch.CheckBox31 = True Or _
frmSearch.CheckBox32 = True Or frmSearch.CheckBox33 = True Then
ws.Range("K2:AQ1500").AutoFilter Field:="fld", Criteria1:="<>"
End If
End Sub
答案 0 :(得分:0)
您的代码在If frmSearch.CheckBox1 = True = True
处的“ True”是否过多?
部分?
尝试以下。
Private Sub FilterButton_Click()
Dim wb As Workbook: Set wb = ThisWorkbook
Dim ws As Worksheet
Set ws = wb.Sheets("Projects")
Dim fld As Long
For fld = 11 To 43
Next
If frmSearch.CheckBox1 = True Or _
frmSearch.CheckBox2 = True Or frmSearch.CheckBox3 = True Or _
frmSearch.CheckBox4 = True Or frmSearch.CheckBox5 = True Or _
frmSearch.CheckBox6 = True Or frmSearch.CheckBox7 = True Or _
frmSearch.CheckBox8 = True Or frmSearch.CheckBox9 = True Or _
frmSearch.CheckBox10 = True Or frmSearch.CheckBox11 = True Or _
frmSearch.CheckBox12 = True Or frmSearch.CheckBox13 = True Or _
frmSearch.CheckBox14 = True Or frmSearch.CheckBox15 = True Or _
frmSearch.CheckBox16 = True Or frmSearch.CheckBox17 = True Or _
frmSearch.CheckBox18 = True Or frmSearch.CheckBox19 = True Or _
frmSearch.CheckBox20 = True Or frmSearch.CheckBox21 = True Or _
frmSearch.CheckBox22 = True Or frmSearch.CheckBox23 = True Or _
frmSearch.CheckBox24 = True Or frmSearch.CheckBox25 = True Or _
frmSearch.CheckBox26 = True Or frmSearch.CheckBox27 = True Or _
frmSearch.CheckBox28 = True Or frmSearch.CheckBox29 = True Or _
frmSearch.CheckBox30 = True Or frmSearch.CheckBox31 = True Or _
frmSearch.CheckBox32 = True Or frmSearch.CheckBox33 = True Then
ws.Range("K2:AQ1500").AutoFilter Field:="fld", Criteria1:="<>"
End If
End Sub
答案 1 :(得分:0)
所以我不知道您的UserForm是什么样子,但是如果可以的话,我建议将这些CheckBoxes移到一个Frame上,这样您就可以更轻松地遍历它们……如下:
Private Sub FilterButton_Click()
Dim wb As Workbook: Set wb = ThisWorkbook
Dim ws As Worksheet
Set ws = wb.Sheets("Projects")
Dim i As Integer
' For each child control in the frame
For i = 0 To Frame1.Controls.Count - 1
' If the child control is a check box
If TypeOf Frame1.Controls.Item(i) Is CheckBox Then
' If the CheckBox is checked
If Frame1.Controls.Item(i).Value Then
'Your code here...
Debug.Print (i & " " & Frame1.Controls.Item(i).Name)
End If
End If
Next i
End Sub
对于应用过滤器,听起来每个复选框都应该链接到您范围内的一列...(这意味着Checkbox1 ==>'K'?)。我将这样做:(替换为'您的代码从上面来)
' Add a filter to the column at the index of 'i' (+1 so we aren't using base 0)
ws.Range("K2:AQ1500").AutoFilter Field:=i + 1, Criteria1:="<>"
请,如果您不理解该代码,请询问!我很乐意解释任何事情。请记住,复制/粘贴不起作用! :D