Excel VBA - Pivot table filter multiple criteria

Excel VBA - Pivot table filter multiple criteria

Filtering a Pivot Table with multiple criteria in Excel VBA can be done using the PivotFilters property of a PivotField. This allows you to set various filtering options such as multiple items, specific criteria, or even date ranges.

Here's a detailed guide and example on how to filter a Pivot Table with multiple criteria using VBA:

Example VBA Code

Assume you have a Pivot Table in Excel, and you want to apply multiple criteria to a specific field in that Pivot Table.

1. Filter by Multiple Items

To filter a Pivot Table field to show only specific items, you can use the PivotFilters property with a combination of PivotItems.

Sub FilterPivotTableMultipleItems() Dim ws As Worksheet Dim pvtTable As PivotTable Dim pvtField As PivotField Dim itemArray As Variant Dim i As Long ' Set your worksheet and pivot table Set ws = ThisWorkbook.Sheets("Sheet1") ' Change to your sheet name Set pvtTable = ws.PivotTables("PivotTable1") ' Change to your pivot table name ' Set the Pivot Field to filter Set pvtField = pvtTable.PivotFields("Category") ' Change to your field name ' Define the items you want to include in the filter itemArray = Array("Item1", "Item2", "Item3") ' Change to your items ' Clear existing filters pvtField.ClearAllFilters ' Apply multiple item filter With pvtField .EnableMultiplePageItems = True For i = LBound(itemArray) To UBound(itemArray) .PivotItems(itemArray(i)).Visible = True Next i ' Hide all items not in the list For i = 1 To .PivotItems.Count If IsError(Application.Match(.PivotItems(i).Name, itemArray, 0)) Then .PivotItems(i).Visible = False End If Next i End With End Sub 

2. Filter by Specific Criteria

To filter a Pivot Table field based on specific criteria (e.g., greater than or less than a certain value), you use the PivotFilters property.

Sub FilterPivotTableByCriteria() Dim ws As Worksheet Dim pvtTable As PivotTable Dim pvtField As PivotField Dim filterField As String Dim filterCriteria As String ' Set your worksheet and pivot table Set ws = ThisWorkbook.Sheets("Sheet1") ' Change to your sheet name Set pvtTable = ws.PivotTables("PivotTable1") ' Change to your pivot table name ' Set the Pivot Field to filter Set pvtField = pvtTable.PivotFields("SalesAmount") ' Change to your field name ' Clear existing filters pvtField.ClearAllFilters ' Apply criteria filter (e.g., SalesAmount greater than 1000) pvtField.PivotFilters.Add _ Type:=xlValueIsGreaterThan, _ Value1:=1000 ' Inform the user MsgBox "Pivot Table filtered by criteria." End Sub 

Explanation

  1. Filter by Multiple Items:

    • Define Items: Use an array to list the items you want to include in the filter.
    • Apply Filter: Loop through the items in the array and set each item's Visible property to True. Set other items' Visible property to False.
    • Clear Filters: Ensure you clear any existing filters before applying new ones.
  2. Filter by Specific Criteria:

    • Criteria Filter: Use PivotFilters.Add to apply filters based on specific criteria. The Type parameter defines the filter type (e.g., greater than, less than, between).
    • Clear Filters: Clear existing filters before applying new criteria.

Additional Notes

  • Field Names: Ensure the field names in the VBA code match those in your Pivot Table.
  • Error Handling: Consider adding error handling to manage cases where fields or items might not exist.
  • Pivot Table Name: Change "PivotTable1" to the actual name of your Pivot Table.

This VBA code will help you set up complex filters for Pivot Tables using multiple criteria. If you have specific requirements or additional questions, let me know!

Examples

  1. How to apply multiple filter criteria to a Pivot Table in Excel VBA?

    • Description: This query involves using VBA to apply multiple filter criteria to a Pivot Table.
    • Code:
      Sub FilterPivotTableMultipleCriteria() Dim ws As Worksheet Dim pvtTable As PivotTable Set ws = ThisWorkbook.Sheets("Sheet1") Set pvtTable = ws.PivotTables("PivotTable1") With pvtTable.PivotFields("Category") .ClearAllFilters .PivotFilters.Add2 Type:=xlCaptionEquals, Value1:="A" .PivotFilters.Add2 Type:=xlCaptionEquals, Value1:="B" End With End Sub 
      Description: Applies multiple filter criteria ("A" and "B") to the "Category" field in a Pivot Table.
  2. How to filter Pivot Table by multiple values using VBA?

    • Description: This query addresses filtering a Pivot Table by multiple values in VBA.
    • Code:
      Sub FilterPivotTableByMultipleValues() Dim ws As Worksheet Dim pvtTable As PivotTable Set ws = ThisWorkbook.Sheets("Sheet1") Set pvtTable = ws.PivotTables("PivotTable1") With pvtTable.PivotFields("Product") .ClearAllFilters .EnableMultiplePageItems = True .CurrentPageList = Array("Product1", "Product2", "Product3") End With End Sub 
      Description: Filters the "Product" field to show multiple selected values.
  3. How to set up a date range filter for a Pivot Table in VBA?

    • Description: This query involves setting up a date range filter for a Pivot Table using VBA.
    • Code:
      Sub FilterPivotTableDateRange() Dim ws As Worksheet Dim pvtTable As PivotTable Set ws = ThisWorkbook.Sheets("Sheet1") Set pvtTable = ws.PivotTables("PivotTable1") With pvtTable.PivotFields("Date") .ClearAllFilters .PivotFilters.Add2 Type:=xlDateBetween, Value1:="2024-01-01", Value2:="2024-12-31" End With End Sub 
      Description: Applies a date range filter to the "Date" field from January 1, 2024, to December 31, 2024.
  4. How to apply multiple value filters to a Pivot Table in Excel VBA?

    • Description: This query explores applying multiple value filters in VBA.
    • Code:
      Sub FilterPivotTableByValues() Dim ws As Worksheet Dim pvtTable As PivotTable Set ws = ThisWorkbook.Sheets("Sheet1") Set pvtTable = ws.PivotTables("PivotTable1") With pvtTable.PivotFields("Sales") .ClearAllFilters .PivotFilters.Add2 Type:=xlValueBetween, Value1:=5000, Value2:=10000 End With End Sub 
      Description: Filters the "Sales" field to show values between 5000 and 10000.
  5. How to clear filters and apply new criteria to a Pivot Table using VBA?

    • Description: This query deals with clearing existing filters and applying new criteria in VBA.
    • Code:
      Sub ClearAndApplyNewFilters() Dim ws As Worksheet Dim pvtTable As PivotTable Set ws = ThisWorkbook.Sheets("Sheet1") Set pvtTable = ws.PivotTables("PivotTable1") ' Clear all existing filters pvtTable.PivotFields("Category").ClearAllFilters pvtTable.PivotFields("Product").ClearAllFilters ' Apply new filters With pvtTable.PivotFields("Category") .PivotFilters.Add2 Type:=xlCaptionEquals, Value1:="A" End With With pvtTable.PivotFields("Product") .PivotFilters.Add2 Type:=xlCaptionEquals, Value1:="Product1" End With End Sub 
      Description: Clears existing filters from "Category" and "Product" fields and applies new filters.
  6. How to filter a Pivot Table based on a dynamic list of values in VBA?

    • Description: This query involves filtering a Pivot Table based on a dynamic list of values.
    • Code:
      Sub FilterPivotTableDynamicList() Dim ws As Worksheet Dim pvtTable As PivotTable Dim filterValues As Variant Set ws = ThisWorkbook.Sheets("Sheet1") Set pvtTable = ws.PivotTables("PivotTable1") ' Define dynamic list of filter values filterValues = Array("Value1", "Value2", "Value3") With pvtTable.PivotFields("FieldName") .ClearAllFilters .EnableMultiplePageItems = True .CurrentPageList = filterValues End With End Sub 
      Description: Uses a dynamic list to filter the "FieldName" field.
  7. How to apply a filter to a Pivot Table based on a cell value in VBA?

    • Description: This query addresses filtering a Pivot Table based on a cell value.
    • Code:
      Sub FilterPivotTableByCellValue() Dim ws As Worksheet Dim pvtTable As PivotTable Dim filterValue As String Set ws = ThisWorkbook.Sheets("Sheet1") Set pvtTable = ws.PivotTables("PivotTable1") ' Get filter value from a cell filterValue = ws.Range("A1").Value With pvtTable.PivotFields("Category") .ClearAllFilters .PivotFilters.Add2 Type:=xlCaptionEquals, Value1:=filterValue End With End Sub 
      Description: Filters the "Category" field based on a value specified in cell A1.
  8. How to use VBA to filter a Pivot Table with multiple criteria from different fields?

    • Description: This query involves applying multiple criteria from different fields.
    • Code:
      Sub FilterPivotTableMultipleFields() Dim ws As Worksheet Dim pvtTable As PivotTable Set ws = ThisWorkbook.Sheets("Sheet1") Set pvtTable = ws.PivotTables("PivotTable1") ' Filter by multiple criteria from different fields With pvtTable.PivotFields("Category") .ClearAllFilters .PivotFilters.Add2 Type:=xlCaptionEquals, Value1:="A" End With With pvtTable.PivotFields("Product") .ClearAllFilters .PivotFilters.Add2 Type:=xlCaptionEquals, Value1:="Product1" End With End Sub 
      Description: Applies filters to the "Category" and "Product" fields.
  9. How to programmatically set Pivot Table filters to show top 10 items in VBA?

    • Description: This query involves setting a filter to show the top 10 items in a Pivot Table.
    • Code:
      Sub FilterTop10PivotTable() Dim ws As Worksheet Dim pvtTable As PivotTable Set ws = ThisWorkbook.Sheets("Sheet1") Set pvtTable = ws.PivotTables("PivotTable1") With pvtTable.PivotFields("Sales") .ClearAllFilters .PivotFilters.Add2 Type:=xlTopCount, Value1:=10 End With End Sub 
      Description: Filters the "Sales" field to show the top 10 items.
  10. How to filter a Pivot Table to exclude specific values using VBA?

    • Description: This query involves excluding specific values from a Pivot Table filter.
    • Code:
      Sub ExcludeValuesFromPivotTable() Dim ws As Worksheet Dim pvtTable As PivotTable Set ws = ThisWorkbook.Sheets("Sheet1") Set pvtTable = ws.PivotTables("PivotTable1") With pvtTable.PivotFields("Category") .ClearAllFilters .PivotFilters.Add2 Type:=xlCaptionDoesNotEqual, Value1:="ExcludeValue" End With End Sub 
      Description: Applies a filter to the "Category" field to exclude rows with the value "ExcludeValue."

More Tags

trigger.io vpn lambdaj ssl tornado stackdriver viewaction woocommerce arm powershell-5.0

More Programming Questions

More Weather Calculators

More Organic chemistry Calculators

More Electronics Circuits Calculators

More Auto Calculators