code to filter values using multiple criteria

Salman Khan

I am making a small program. On main sheet, there are two combo boxes.

What I am trying to do, if I select value from each combo box, it will filter the data. But I am facing a small problem. I want a ALL value in both combo boxes and upon selecting that value it shouldn't filter that column.

So far my code is like this:

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
user4039065

Currently, you do not set a filter on field 1 if ComboBox1 is a zero-length string; extend that condition to include 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

You've declared ws and assigned it to Sheets("Graphical Summary"); you might as well use it.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Using FILTER Function With Multiple Criteria

Multiple Values using single Criteria

Python: Filter values with multiple criteria and lists

Returning multiple values using multiple matching criteria

filter out multiple criteria using excel vba

Using Filter Formula with Multiple Criteria and Allowing Blank Criteria

countif unique values using multiple criteria

Using Countifs to count distinct values and multiple criteria?

Return and sum multiple values using a wildcard criteria

How to convert if-else if chaining block code with multiple filter criteria into single filter criteria with null check?

Filter column for multiple values but only select the last one for one criteria

FILTER with multiple criteria (and blank values) with different range sizes

Set multiple values as pivot table filter criteria in vba

How to filter an array in PHP by fields with empty values and multiple criteria?

Advanced filter with multiple criteria

Filter Array by Multiple Criteria

Filter GetFiles() on multiple criteria

Filter Multiple Values using pandas

Filter dataframe using multiple values

Elegant way to filter records based on multiple criteria using R

Pandas filter for column names using multiple like criteria

Conditional filter using Knex.js and SQL with multiple search criteria

Using nested lapply to filter for criteria and create multiple data frames

MS Access - Using Form Inputs to Filter on Multiple Criteria in a Query

Is there any way I can filter with multiple criteria using jQuery?

Filter data points on a map using R Leaflet and Shiny (multiple criteria)

Getting a key of Nested Dic using multiple values criteria

Multi-criteria search and returning multiple values using array functions

How to sum multiple values using offset based off criteria?