Multiple Filter Criteria for blanks and numbers using wildcard on same field just doesn't work

前端 未结 2 1412
梦如初夏
梦如初夏 2021-01-24 15:25

Despite the number of questions on this topic I haven\'t been able to find the resolution to my issue (which may or may not be my fault).

I need to autofilter a range to

相关标签:
2条回答
  • 2021-01-24 15:45

    Consider add ' in each of your data.

    Example :
    100 => '100
    
    0 讨论(0)
  • 2021-01-24 16:03

    Anytime you run into a restriction on what you can do with the Range.AutoFilter method, simply build a dictionary of matching criteria using VBA's text, number and/or date manipulation and apply the keys of the dictionary to an AutoFilter operation as an array.

    Sub wildcard_Number_Filter()
        Dim a As Long, aTMPs As Variant, dVALs As Object
    
        Set dVALs = CreateObject("Scripting.Dictionary")
        dVALs.CompareMode = vbTextCompare
    
        With Worksheets("Sheet1")
            If .AutoFilterMode Then .AutoFilterMode = False
            With .Cells(1, 1).CurrentRegion
                'build a dictionary so the keys can be used as the array filter
                aTMPs = .Columns(2).Cells.Value2
                For a = LBound(aTMPs, 1) + 1 To UBound(aTMPs, 1)
                    Select Case True
                        Case Not CBool(Len(aTMPs(a, 1)))
                            dVALs.Item(Chr(61)) = Chr(61)   'blanks
                        Case CStr(aTMPs(a, 1)) Like "614*"
                            'The set of numbers have to be strings in the array
                            If Not dVALs.Exists(aTMPs(a, 1)) Then _
                                dVALs.Add Key:=CStr(aTMPs(a, 1)), Item:=aTMPs(a, 1)
                        Case Else
                            'no match. do nothing
                    End Select
                Next a
    
                'test the array
                'Dim k As Variant
                'For Each k In dVALs.Keys
                '    Debug.Print k & " - " & dVALs.Item(k)
                'Next k
    
                'filter on column B if dictionary keys exist
                If CBool(dVALs.Count) Then _
                    .AutoFilter Field:=2, Criteria1:=dVALs.Keys, _
                                          Operator:=xlFilterValues, VisibleDropDown:=False
    
                'data is filtered on 614* and blanks (column B)
                'Perform work on filtered data here
    
            End With
            If .AutoFilterMode Then .AutoFilterMode = False
        End With
    
        dVALs.RemoveAll: Set dVALs = Nothing
    End Sub
    

    By using this method, any manipulation you can perform on values to check them for inclusion or exclusion through standard VBA methods can generate a valid array to be used as a filter set. By bulk loading the values from the worksheet into a variant array, the time to read and evaluate individual cells is virtually non-existent.

            
                Before building and applying filter

            
                After applying filter

    0 讨论(0)
提交回复
热议问题