用户表单用于设置多个筛选条件

时间:2013-08-02 21:23:29

标签: excel vba excel-vba excel-2010 userform

我有6个用户表单会弹出并要求您选择要排序的变量:

  1. ClientFilter
  2. CostCenterFilter
  3. UtilityFilter
  4. TypeOfWorkfilter
  5. AnalystFilter
  6. ProjMgrFilter
  7. 在每一个内部,它使用每个变量的if语句。

    Sub UpdateCF()
        Integer_CF = -1
    
        If AU_CF.Value = True Then
            Add_CF String_CF, "AU"
            Range("$B$6:$AU$68").AutoFilter  _
                  Field:=2, Criteria1:=String_CF,   _
                  Operator:=xlFilterValues
        End If
    
        If AULaw_CF.Value = True Then
            Add_CF String_CF, "AULAW"
            Range("$B$6:$AU$68").AutoFilter  _
                  Field:=2, Criteria1:=String_CF,   _
                  Operator:=xlFilterValues
        End If
        ... 
    
    End Sub
    
    Sub Add_CF(String_CF() As String, NewValue As String)
        Integer_CF = Integer_CF + 1
        ReDim Preserve String_CF(Integer_CF)
        String_CF(Integer_CF) = NewValue
    End Sub
    

    除了我希望能够对多个字段进行排序之外,这个工作正常。例如,我希望使用客户端过滤器,然后选择一个变量,然后使用“成本中心用户表单”同时启用这两个排序。

1 个答案:

答案 0 :(得分:1)

我不确定您认为自己有问题的原因 - 您可以使用与上述相同的方法,但将过滤器添加到其他字段。这将过滤两者。

例如(显然你需要适应你的情况):

Sub blah()

    Dim currentFilters_FirstField
    Dim currentFilters_SecondField
    Dim field1_Option1, field1_Option2, field2_Option1, field2_Option2 'just an example
    Dim rng As Range

    field1_Option1 = True
    field1_Option2 = True
    field2_Option1 = True
    field2_Option2 = False

    'prepare your filters from the menu etc
    If field1_Option1 Then appendFilterValue currentFilters_FirstField, "AU"
    If field1_Option2 Then appendFilterValue currentFilters_FirstField, "AULAW"
    If field2_Option1 Then appendFilterValue currentFilters_SecondField, "Whatever1"
    If field2_Option2 Then appendFilterValue currentFilters_SecondField, "Whatever2"

    Set rng = Range("A1:D100") ' range for the filtering etc
    rng.AutoFilter 1, currentFilters_FirstField, xlFilterValues
    rng.AutoFilter 2, currentFilters_SecondField, xlFilterValues 'note the different field this is applying to

End Sub

Sub appendFilterValue(ByRef currentFilters, newFilter)

    If IsArray(currentFilters) Then
        ReDim Preserve currentFilters(0 To UBound(currentFilters) + 1)
    Else
        ReDim currentFilters(0 To 0)
    End If

    currentFilters(UBound(currentFilters)) = newFilter

End Sub