I think I understand your question. This filters out objects that are in column labels or row labels. The last 2 sections of the code is what you want, but im inserting everything so that you can see exactly how it starts, ending with everything that is defined, etc. I definitely took some of this code from other fyi sites.
At the end of the code, "WardClinic_Category" is the column of my data and the column label of the pivot table. Same thing for IVUDDCIndicator (its column is in my data, but in the row label of the pivot table).
I hope this helps others ... I find it very difficult to find the code that made this the βright wayβ, and not use code that looks like a macro recorder.
Sub CreatingPivotTableNewData() 'Creating pivot table Dim PvtTbl As PivotTable Dim wsData As Worksheet Dim rngData As Range Dim PvtTblCache As PivotCache Dim wsPvtTbl As Worksheet Dim pvtFld As PivotField 'determine the worksheet which contains the source data Set wsData = Worksheets("Raw_Data") 'determine the worksheet where the new PivotTable will be created Set wsPvtTbl = Worksheets("3N3E") 'delete all existing Pivot Tables in the worksheet 'in the TableRange1 property, page fields are excluded; to select the entire PivotTable report, including the page fields, use the TableRange2 property. For Each PvtTbl In wsPvtTbl.PivotTables If MsgBox("Delete existing PivotTable!", vbYesNo) = vbYes Then PvtTbl.TableRange2.Clear End If Next PvtTbl 'A Pivot Cache represents the memory cache for a PivotTable report. Each Pivot Table report has one cache only. Create a new PivotTable cache, and then create a new PivotTable report based on the cache. 'set source data range: Worksheets("Raw_Data").Activate Set rngData = wsData.Range(Range("A1"), Range("H1").End(xlDown)) 'Creates Pivot Cache and PivotTable: Worksheets("Raw_Data").Activate ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=rngData.Address, Version:=xlPivotTableVersion12).CreatePivotTable TableDestination:=wsPvtTbl.Range("A1"), TableName:="PivotTable1", DefaultVersion:=xlPivotTableVersion12 Set PvtTbl = wsPvtTbl.PivotTables("PivotTable1") 'Default value of ManualUpdate property is False so a PivotTable report is recalculated automatically on each change. 'Turn this off (turn to true) to speed up code. PvtTbl.ManualUpdate = True 'Adds row and columns for pivot table PvtTbl.AddFields RowFields:="VerifyHr", ColumnFields:=Array("WardClinic_Category", "IVUDDCIndicator") 'Add item to the Report Filter PvtTbl.PivotFields("DayOfWeek").Orientation = xlPageField 'set data field - specifically change orientation to a data field and set its function property: With PvtTbl.PivotFields("TotalVerified") .Orientation = xlDataField .Function = xlAverage .NumberFormat = "0.0" .Position = 1 End With 'Removes details in the pivot table for each item Worksheets("3N3E").PivotTables("PivotTable1").PivotFields("WardClinic_Category").ShowDetail = False 'Removes pivot items from pivot table except those cases defined below (by looping through) For Each PivotItem In PvtTbl.PivotFields("WardClinic_Category").PivotItems Select Case PivotItem.Name Case "3N3E" PivotItem.Visible = True Case Else PivotItem.Visible = False End Select Next PivotItem 'Removes pivot items from pivot table except those cases defined below (by looping through) For Each PivotItem In PvtTbl.PivotFields("IVUDDCIndicator").PivotItems Select Case PivotItem.Name Case "UD", "IV" PivotItem.Visible = True Case Else PivotItem.Visible = False End Select Next PivotItem 'turn on automatic update / calculation in the Pivot Table PvtTbl.ManualUpdate = False End Sub
source share