Tip: Other languages are Google-Translated. You can visit the English version of this link.
Log in


How to filter Pivot table based on a specific cell value in Excel?

Normally, we are filtering data in a Pivot Table by checking values from the drop-down list as the left screenshot shown. If you want to make a Pivot Table more dynamic by filtering, you can try to filter it based on value in a specific cell. The VBA method in this article will help you solving the problem.

Filter Pivot Table based on a specific cell value with VBA code

Easily select entire rows based on cell value in a certian column:

The Select Specific Cells utility of Kutools for Excel can help you quickly select entire rows based on cell value in a certian column in Excel as below screenshot shown. After selecting all rows based on cell value, you can manually move or copy them to a new location as you need in Excel.

Kutools for Excel: with more than 200 handy Excel add-ins, free to try with no limitation in 60 days. Download the free trial Now!

Excel Productivity Tools

Office Tab: Bring powerful tabs to Office (include Excel), just like Chrome, Safari, Firefox and Internet Explorer. Save you half the time, and reduce thousands of mouse clicks for you. 30-day Unlimited Free Trial

Kutools for Excel: Save 70% of your time and solve 80% Excel problems for you. 300+ advanced features designed for 1500+ work scenario, make Excel much easy and increase productivity immediately. 60-day Unlimited Free Trial

Filter Pivot Table based on a specific cell value with VBA code

The following VBA code can help you filtering a Pivot Table based on a specific cell value in Excel. Please do as follows.

1. Please enter a value you will filter Pivot Table based on into a cell in advance (here I select cell H6).

2. Open the worksheet contains the Pivot Table you will filter by cell value. Then right click the sheet tab and select View Code from the context menu. See screenshot:

3. In the opening Microsoft Visual Basic for Applications window, copy below VBA code into the Code window.

VBA code: Filter Pivot Table based on cell value

Private Sub Worksheet_Change(ByVal Target As Range)
'Update by Extendoffice 20180702
    Dim xPTable As PivotTable
    Dim xPFile As PivotField
    Dim xStr As String
    On Error Resume Next
    If Intersect(Target, Range("H6:H7")) Is Nothing Then Exit Sub
    Application.ScreenUpdating = False
    Set xPTable = Worksheets("Sheet1").PivotTables("PivotTable2")
    Set xPFile = xPTable.PivotFields("Category")
    xStr = Target.Text
    xPFile.CurrentPage = xStr
    Application.ScreenUpdating = True
End Sub

Notes: In the code,

1) “Sheet1” is the name of the worksheet.

2) “PivotTable2” is the name of the Pivot Table.

3) The filtering field in the pivot table is called "Category".

4) The value you want to filter the pivot table is placed in cell H6.

You can change the above variable values as you need.v

4. Press the Alt + Q keys to close the Microsoft Visual Basic for Applications window.

Then the Pivot Table is filtering based on the value in cell H6 as below screenshot shown:

You can change the cell value to others as you need.

Note: Values you type in cell H6 should exactly match the values in the Category drop down list of Pivot Table.

Office Tab - Tabbed Browsing, Editing, and Managing of Workbooks in Excel:

Office Tab brings the tabbed interface as seen in web browsers such as Google Chrome, Internet Explorer new versions and Firefox to Microsoft Excel. It will be a time-saving tool and irreplaceble in your work. See below demo:

Click for free trial of Office Tab!

Office Tab for Excel

Related articles:

Excel Productivity Tools

Ribbon of Excel (with Kutools for Excel installed)

300+ Advanced Features Increase Your Productivity by 70%, and Help You To Stand Out From Crowd!

Would you like to complete your daily work quickly and perfectly? Kutools For Excel brings 300+ cool and powerful advanced features (Combine workbooks, sum by color, split cell contents, convert date, and so on...) for 1500+ work scenarios, helps you solve 80% Excel problems.

  • Deal with all complicated tasks in seconds, help to enhance your work ability, get success from the fierce competition, and never worry about being fired.
  • Save a lot of work time, leave much time for you to love and care the family and enjoy a comfortable life now.
  • Reduce thousands of keyboard and mouse clicks every day, relieve your tired eyes and hands, and give you a healthy body.
  • Become an Excel expert in 3 minutes, and get admiring glance from your colleagues or friends.
  • No longer need to remember any painful formulas and VBA codes, have a relaxing and pleasant mind, give you a thrill you've never had before.
  • Spend only $39, but worth than $4000 training of others. Being used by 110,000 elites and 300+ well-known companies.
  • 60-day unlimited free trial. 60-day money back guarantee. Free upgrade and support for 2 years. Buy once, use forever.
  • Change the way you work now, and give you a better life immediately!

Office Tab Brings Efficient And Handy Tabs to Office (include Excel), Just Like Chrome, Firefox, And New IE

  • Increases your productivity by 50% when viewing and editing multiple documents.
  • Reduce hundreds of mouse clicks for you every day, say goodbye to mouse hand.
  • Open and create documents in new tabs of same window, rather than in new windows.
  • Help you work faster and easily stand out from the crowd! One second to switch between dozens of open documents!
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Justin · 8 months ago
    How would you do this with a report filter that has a hierarchy?

  • To post as a guest, your comment is unpublished.
    Daniel · 9 months ago
    Using this code (updated for my variables of course), when changing the field, the filter changes momentarily to the correct one, and then clears itself almost immediately. Trying to figure out why it's doing this (wondering if it has something to do with the ClearAllFilters at the end of the sub?)