代码使用多个条件过滤值

我正在制作一个小程序。 在主表上,有两个combobox。

我正在尝试做的,如果我从每个combobox中select值,它会过滤数据。 但是我面临一个小问题。 我想在这两个combobox中的所有值,并select该值时,不应筛选该列。

到目前为止,我的代码是这样的:

Sub submit() Dim ws As Worksheet, tbl As ListObject, rng As Range Set ws = Sheets("Graphical Summary") Set tbl = ws.ListObjects("Table5") Set rng = tbl.DataBodyRange With tbl .Range.AutoFilter Field:=1 .Range.AutoFilter Field:=3 End With With rng If Sheets("Graphical Summary").ComboBox1.Value = "All" Then .AutoFilter Field:=2, Criteria1:=Sheets("Graphical Summary").ComboBox2.Value If Sheets("Graphical Summary").ComboBox1.Value <> vbNullString Then .AutoFilter Field:=1, Criteria1:=Sheets("Graphical Summary").ComboBox1.Value If Sheets("Graphical Summary").ComboBox2.Value <> vbNullString Then .AutoFilter Field:=2, Criteria1:=Sheets("Graphical Summary").ComboBox2.Value End With End Sub 

目前,如果ComboBox1是零长度string,则不要在字段1上设置filter; 扩展该条件以包含ALL

 With rng If ws.ComboBox1.Value <> vbNullString And ws.ComboBox1.Value <> "All" Then _ .AutoFilter Field:=1, Criteria1:=ws.ComboBox1.Value If ws.ComboBox2.Value <> vbNullString Then _ .AutoFilter Field:=2, Criteria1:=ws.ComboBox2.Value End With 

您已经声明了ws并将其分配给了表格(“graphics摘要”); 你也可以使用它。