使用自动过滤和用户表单选择多个项目

时间:2017-07-31 14:54:27

标签: excel vba excel-vba

我需要一种方法来从userform复选框中选择多个项目。我相信我有一个逻辑错误,一旦声明为真,逻辑就不会继续。

以下是我目前的代码:

If flt_type_cb1.Value = True Then
    WholeSheetRange.AutoFilter Field:=7, Criteria1:="=DOM"
ElseIf flt_type_cb2.Value = True Then
    WholeSheetRange.AutoFilter Field:=7, Criteria1:="=AK"
ElseIf flt_type_cb3.Value = True Then
    WholeSheetRange.AutoFilter Field:=7, Criteria1:="=HI", Operator:=xlOr, Criteria2:="HI-LH"
ElseIf flt_type_cb4.Value = True Then
    WholeSheetRange.AutoFilter Field:=7, Criteria1:="=INTL-ASIA", Operator:=xlOr, Criteria2:="INTL-ASIA-PJ"
ElseIf flt_type_cb5.Value = True Then
    WholeSheetRange.AutoFilter Field:=7, Criteria1:="=PS"
ElseIf flt_type_cb6.Value = True Then
    WholeSheetRange.AutoFilter Field:=7, Criteria1:="=GU", Operator:=xlOr, Criteria2:="GUMHNL"
ElseIf flt_type_cb7.Value = True Then
    WholeSheetRange.AutoFilter Field:=7, Criteria1:="=LATBUS"
ElseIf flt_type_cb8.Value = True Then
    WholeSheetRange.AutoFilter Field:=7, Criteria1:="=INTL", Operator:=xlOr, Criteria2:="INTL-PJ"
End If

我希望能够同时选择DOM和PS。

1 个答案:

答案 0 :(得分:0)

我遵循了if语句路径,即使它可能不是最有效的。

我的代码如下:

Dim flight_type_array As Variant

flight_type_array = Array("DOM", "AK", "HI", "HI-LH", "INTL-ASIA", "INTL-ASIA-PJ", "PS", "GU", "GUMHNL", "LATBUS", "INTL", "INTL-PJ")

If flt_type_cb1.Value = False Then
    flight_type_array(0) = ""
End If
If flt_type_cb2.Value = False Then
    flight_type_array(1) = ""
End If
If flt_type_cb3.Value = False Then
    flight_type_array(2) = ""
    flight_type_array(3) = ""
End If
If flt_type_cb4.Value = False Then
    flight_type_array(4) = ""
    flight_type_array(5) = ""
End If
If flt_type_cb5.Value = False Then
    flight_type_array(6) = ""
End If
If flt_type_cb6.Value = False Then
    flight_type_array(7) = ""
    flight_type_array(8) = ""
End If
If flt_type_cb7.Value = False Then
    flight_type_array(9) = ""
End If
If flt_type_cb8.Value = False Then
    flight_type_array(10) = ""
    flight_type_array(11) = ""
End If

WholeSheetRange.AutoFilter Field:=7, Criteria1:=flight_type_array, Operator:=xlFilterValues