You are here:

Excel/Filter on pivot table data field


Hello Mr. Ogilvy -

I am using Excel 2010 and creating a pivot table using VBA.  Within the pivot table, I have the following code:

       ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
         Rpt_Data_Range_Name, Version:=xlPivotTableVersion14).CreatePivotTable _
         TableDestination:=PTable_Placement, TableName:=PTable_Name, _
       With ActiveSheet.PivotTables(PTable_Name).PivotFields(Mgr_Label)
         .Orientation = xlRowField
         .Position = 1
       End With
       ActiveSheet.PivotTables(PTable_Name).AddDataField ActiveSheet.PivotTables(PTable_Name).PivotFields(EE_Label), _
         "Count of " & EE_Label, xlCount
       With ActiveSheet.PivotTables(PTable_Name).PivotFields("Count of " & EE_Label)
         .Caption = "EE Count"
       End With
       ActiveSheet.PivotTables(PTable_Name).AddDataField ActiveSheet.PivotTables(PTable_Name).PivotFields(BY_Label), _
         "Sum of " & BY_Label, xlSum
       With ActiveSheet.PivotTables(PTable_Name).PivotFields("Sum of " & BY_Label)
         .Caption = BY_Label & " Compl Rate"
         .Function = xlAverage
         .NumberFormat = "0%"
       End With
       ActiveSheet.PivotTables(PTable_Name).AddDataField ActiveSheet.PivotTables(PTable_Name).PivotFields(MY_Label), _
         "Sum of " & MY_Label, xlSum
       With ActiveSheet.PivotTables(PTable_Name).PivotFields("Sum of " & MY_Label)
         .Caption = MY_Label & " Compl Rate"
         .Function = xlAverage
         .NumberFormat = "0%"
       End With
       ActiveSheet.PivotTables(PTable_Name).AddDataField ActiveSheet.PivotTables(PTable_Name).PivotFields(MYU_Label), _
         "Sum of " & MYU_Label, xlSum
       With ActiveSheet.PivotTables(PTable_Name).PivotFields("Sum of " & MYU_Label)
         .Caption = MYU_Label & " Compl Rate"
         .Function = xlAverage
         .NumberFormat = "0%"
       End With
       ActiveSheet.PivotTables(PTable_Name).AddDataField ActiveSheet.PivotTables(PTable_Name).PivotFields(EY_Label), _
         "Sum of " & EY_Label, xlSum
       With ActiveSheet.PivotTables(PTable_Name).PivotFields("Sum of " & EY_Label)
         .Caption = EY_Label & " Compl Rate"
         .Function = xlAverage
         .NumberFormat = "0%"
       End With
       With ActiveSheet.PivotTables(PTable_Name).PivotFields(Div_Label)
         .Orientation = xlPageField
         .Position = 1
       End With
       With ActiveSheet.PivotTables(PTable_Name).PivotFields(Loc_Label)
         .Orientation = xlPageField
         .Position = 1
       End With

I would like the end result to be a pivot table containing 6 columns, but each one having a filter:  Manager, Employee Count, Avg of BY (value = 0 or 1 depending on completion of a task), Avg of MY, Avg of MYU, Avg of EY, with a couple of page filters for Division and Location.

I assume that the use of "adddatafield" is preventing the BY column from having a filter available.  How can I overcome this?

I have a sample data workbook available if it would help.


You can send the workbook to

Make sure it has some sample data in it that can be used to create the pivottable - in otherwords, if I run the macro it should create the same pivottable you are looking at and then I can see what problem you are describing.  

Tom Ogilvy  
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here


All Answers

Answers by Expert:

Ask Experts


Tom Ogilvy


Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]


Extensive experience.

Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2017 All rights reserved.

[an error occurred while processing this directive]