1

I have multiple columns in my Data Table and 3 of it are my preferred fields to filter the data.

Here's the data table with the sheet name of "Data"

IMAGE

and heres my code from another sheet named as "Analysis" were the user will input their preferred Date, Line and market to filter the data table in Sheet "Data"

    Dim lngStart As Date, lngEnd As Date
    Dim LineNum As Integer
    Dim MarketDesc As String

    lngStart = Range("B6").value
    lngEnd = Range("B7").value
    LineNum = Range("D7").value 
    MarketDesc = Range("D8").value

    With Sheets("Data").Range("B6:Z150")
        .AutoFilter Field:=2, _
          Criteria1:=">=" & lngStart, _
          operator:=xlAnd, _
          Criteria2:="<=" & lngEnd

        .AutoFilter Field:=4, _
          Criteria1:="=" & LineNum

        .AutoFilter Field:=5, _
          Criteria1:="=" & MarketDesc


    End With

The Error is If the column has different data like in Market. Where it contains Local and Export. It suddenly dont filter the table where My preferred data is

Line: 2, Market: Local, Category: Instant Mami, Variant: Chicken.

The output should display the data just like the above example But in the picture it shows Market: Local and Export

  • 12/1/2018 as start date
  • 12/7/2018 as end date
  • 2 As line
  • Export As Market

The table will filter based on the preferences

4
  • So where is your problem? What is your question? Do you get any errors? What is wrong with your code? • You didn't ask a question yet. Note that posting a requirement is not asking a question.
    – Pᴇʜ
    Feb 1, 2019 at 7:31
  • I updated the question
    – King RG
    Feb 1, 2019 at 7:40
  • Your code looks fine besides that you filter twice for Field:=5. Does it work if you do that filtering manuall? Are you sure your dates in the date column are real numeric dates (no strings). Can you give us the result of Debug.Print lngStart, lngEnd, LineNum, MarketDesc
    – Pᴇʜ
    Feb 1, 2019 at 8:06
  • 1
    Looking at your image, you can see that there is no filter set on col. E. As your code shows that you set a filter to it: Are you sure that the code was hit? You could set a breakpoint to the start of the code and execute it step by step.
    – FunThomas
    Feb 1, 2019 at 8:31

1 Answer 1

1

I was able to filter the data and examine the filter criteria with the code below:

Option Explicit

Sub SetFilter()
    'Dim lngStart As Date, lngEnd As Date
    Dim lngStart As Long, lngEnd As Long
    Dim LineNum As Integer
    Dim MarketDesc As String

    lngStart = DateSerial(2018, 12, 1)   'Range("B6").Value
    lngEnd = DateSerial(2018, 12, 15)    'Range("B7").Value
    LineNum = 2                          'Range("D7").Value
    MarketDesc = "Export"                'Range("D8").Value

    With Sheets("Data").Range("A5:G170")
        .AutoFilter Field:=2, _
          Criteria1:=">=" & lngStart, _
          Operator:=xlAnd, _
          Criteria2:="<=" & lngEnd

        .AutoFilter Field:=4, _
          Criteria1:="=" & LineNum

        .AutoFilter Field:=5, _
          Criteria1:="=" & MarketDesc

    End With
End Sub

Sub ShowFilters()
    Dim ws As Worksheet, flt As Filter, i As Integer
    Dim operatorString As String
    Set ws = ActiveSheet
    ' If there are filters
    If ws.AutoFilterMode Then
        ' Get each Filter object
        For i = 1 To ws.AutoFilter.Filters.Count
            Set flt = ws.AutoFilter.Filters(i)
            '  And if the filter is on, show its criterion.
            If flt.On Then
                On Error Resume Next
                If flt.Operator Then
                    'search for Autofilter VBA Operator parameters and also for the xlFilterDynamic operator
                    operatorString = Operator2String(flt.Operator)
                    Debug.Print "Column " & i & ": " & flt.Criteria1 & operatorString & flt.Criteria2
                Else
                    Debug.Print "Column " & i & ": " & flt.Criteria1
                End If
                On Error GoTo 0
            End If
        Next
    End If
End Sub

Function Operator2String(OperatorInt As Integer) As String
    Select Case OperatorInt
    Case 1         '1 xlAnd
        Operator2String = " and "
    Case 2         '2 xlOr
        Operator2String = " or "
    Case 3         '3 xlTop10Items
        Operator2String = " NOT-TESTED: xlTop10Items "
    Case 4         '4 xlBottom10Items
        Operator2String = " NOT-TESTED: xlBottom10Items "
    Case 5         '5 xlTop10Percent
        Operator2String = " NOT-TESTED: xlTop10Percent "
    Case 6         '6 xlBottom10Percent
        Operator2String = " NOT-TESTED: xlBottom10Percent "
    Case 7         '7 xlFilterValues
        Operator2String = " NOT-TESTED: xlFilterValues "
    Case 8         '8 xlFilterCellColor
        Operator2String = " NOT-TESTED: xlFilterCellColor "
    Case 9         '9 xlFilterFontColor
        Operator2String = " NOT-TESTED: xlFilterFontColor "
    Case 10        '10 xlFilterIcon
        Operator2String = " NOT-TESTED: xlFilterIcon "
    Case 11        '11 xlFilterDynamic
        Operator2String = " NOT-TESTED: xlFilterDynamic (REVIEW subparameters) "
    Case Else
        Operator2String = " NOT-DEFINED "
    End Select
End Function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.