Pivot TopCount Filter based on cell value

GDunn

Board Regular
Joined
Mar 24, 2009
Messages
51
Hi,

I am trying to use VBA to filter a pivot table value (Top 10 etc) based on a cell value on a seperate sheet.

1. Not sure if I reference Set pvtField = pvtTable.PivotFields("Employer_Name") or Set pvtField = pvtTable.PivotFields("Sum of Value")
2. The command filter (For Each pvtItem In pvtField.PivotItems _
pvtFields.xlTopCount _ pvtFields.Value = filterVal _ Next pvtItem) is causing me a headache.

Is this possible to do? Any help greatly appreciated.

Sub Top_Filter_1()
Dim pvtTable As PivotTable
Dim pvtField As PivotField
Dim pvtItem As PivotItem
Dim filterVal As String

Set pvtTable = Worksheets("T RM LW").PivotTables("PivotTable6")
Set pvtField = pvtTable.PivotFields("Employer_Name")

filterVal = Worksheets("Data").Range("C3")

For Each pvtItem In pvtField.PivotItems
pvtFields.xlTopCount
pvtFields.Value = filterVal

Next pvtItem

End Sub
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Hi GDunn,

For Top10 or TopX, it's easier if you use the PivotFilter property.
Try this untested code....

Code:
Sub Top_Filter_1()
    Dim pvtTable As PivotTable
    Dim pvtField As PivotField
    Dim pvtItem As PivotItem
    Dim filterVal As String
    
    Set pvtTable = Worksheets("T RM LW").PivotTables("PivotTable6")
    Set pvtField = pvtTable.PivotFields("Employer_Name")

    filterVal = Worksheets("Data").Range("C3")

[COLOR="Blue"]    pvtField.PivotFilters.Add Type:=xlTopCount, _
        DataField:=pvtTable.PivotFields("Sum of Value"), Value1:=10[/COLOR]

End Sub
 
Upvote 0
Thanks JS411. I tried something similar previously. This returns a runtime error.
Also, not sure how this filters based on the value in the desired cell. Shouldn't the DataField value be based on FilterVal?
 
Upvote 0
Thanks JS411. I tried something similar previously. This returns a runtime error as the filter already exists. I want to change the filter already in place.
Also, not sure how this filters based on the value in the desired cell. Shouldn't the DataField value be based on FilterVal?
 
Upvote 0
My mistake. What does FilterVal represent?
Is it the number of items IE for Top10 FilterVal=10; or
All number greater than or equal to FilterVal (which might be more or less than 10);
or something else?
 
Upvote 0
filterVal references the cell which stores the value to be filtered (10, 20 etc.)
I cannot reference filterVal as the value for some reason. it asks for a number between 1 and 2######.
 
Upvote 0
filterVal references the cell which stores the value to be filtered (10, 20 etc.)
I cannot reference filterVal as the value for some reason. it asks for a number between 1 and 2######.

I don't understand what you mean by the "Value to be filtered" based on your earlier description of Top10.

Are you trying to filter to show only the Data Value that is in filterVal (not values that are greater or less than)?

BTW if you'r using PivotFilters, you'll use ClearValueFilters to clear any previous filters.
 
Upvote 0
Thanks for your help.
The filterVal is the value of the Top filter. ie. top10, top20 etc.

This variation works if I use the full value field reference.

Sub Top_Filter()
Dim pvtTable As PivotTable
Dim pvtField As PivotField
Dim filterVal As String

Set pvtTable = Worksheets("T RM LW").PivotTables("PivotTable6")
Set pvtField = pvtTable.PivotFields("Employer_Name")
filterVal = Worksheets("Data").Range("C3")
Worksheets("T RM LW").PivotTables("PivotTable6").PivotFields("Employer_Name"). _
ClearAllFilters
pvtField.PivotFilters.Add Type:=xlTopCount, _
DataField:=pvtTable.PivotFields("Sum of Value"), Value1:=Worksheets("Data").Range("C3")

End Sub
 
Last edited:
Upvote 0
Ahhh... now I understand what you meant.

You can either convert the String to a Long Data Type using
Code:
Value1:=CLng(filterVal)

or simply Declare filterVal as a Long Data Type.

Code:
Sub Top_Filter2()
    Dim pvtTable As PivotTable
    Dim pvtField As PivotField
    Dim pvtItem As PivotItem
    Dim filterVal As Long
    
    Set pvtTable = Worksheets("T RM LW").PivotTables("PivotTable6")
    Set pvtField = pvtTable.PivotFields("Employer_Name")

    filterVal = Worksheets("Data").Range("C3")

    With pvtField
        .ClearValueFilters
        .PivotFilters.Add Type:=xlTopCount, _
            DataField:=pvtTable.PivotFields("Sum of Value"), _
            Value1:=filterVal
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,620
Messages
6,120,559
Members
448,970
Latest member
kennimack

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top