I'm trying to figure out the best way to manipulate pivot items based on whether they are present in the data. The below code errors because there happen to be no "Auto-Certified" items this month, but there often are, so I'd like to make the code dynamic enough to handle either situation.
Here's the code in context if needed:
VBA Code:
With ActiveSheet.PivotTables("MyPivot").PivotFields("Certification Status")
.Orientation = xlPageField
.Position = 1
.PivotItems("Auto-Certified").Visible = False '''''''''ERROR HERE
End With
Here's the code in context if needed:
VBA Code:
Sub PivotCreate()
Dim PSheet As Worksheet
Dim DSheet As Worksheet
Dim PCache As PivotCache
Dim PTable As PivotTable
Dim DRange As String
Dim ShtName As String
Dim PName As String
Dim LastRow As Long
Dim LastColumn As Integer
'**********SET UP DATA SHEET AND SOURCE DATA RANGE FOR PIVOT TABLES
ShtName = "Data"
Set DSheet = Worksheets(ShtName)
LastColumn = DSheet.Cells(7, DSheet.Columns.Count).End(xlToLeft).Column
LastRow = DSheet.Cells(DSheet.Rows.Count, "A").End(xlUp).Row
DRange = Range(Cells(6, 1), Cells(LastRow, LastColumn)).Address
'**********CREATE SUMMARY TAB & PIVOT
Sheets.Add Before:=DSheet
Set PSheet = ActiveSheet
PSheet.Name = "Summary"
PName = "SumPivot"
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
ShtName & "!" & DRange, Version:=xlPivotTableVersion15).CreatePivotTable _
TableDestination:=PSheet.Cells(1, 1), TableName:=PName, DefaultVersion _
:=xlPivotTableVersion15
'**********CREATE SUMMARY FIELDS/FILTERS
'Insert Column Fields
With ActiveSheet.PivotTables(PName).PivotFields("Certification Status")
.Orientation = xlPageField
.Position = 1
.PivotItems("Auto-Certified").Visible = False ''''''''''THIS LINE FILTERS OUT THE "AUTO-CERTFIED" RECONS
.PivotItems("Not Required").Visible = False ''''''''''THIS LINE FILTERS OUT THE "NOT REQUIRED" RECONS
End With
With ActiveSheet.PivotTables(PName).PivotFields("Period End Date")
.Orientation = xlPageField
.Position = 2
End With
With ActiveSheet.PivotTables(PName).PivotFields("Preparer Due Date")
.Orientation = xlPageField
.Position = 3
End With
'Insert Row Fields
With ActiveSheet.PivotTables(PName).PivotFields("Approver WDx")
.Orientation = xlRowField
.Position = 1
.PivotItems("WD-10").Position = .PivotItems.Count ''''''''''THIS ITEM MANUALLY SORTS/MOVES THE WD-10 TO THE LAST POSITION WITHIN THE TOTAL COUNT OF ALL PIVOT ITEMS OF "APPROVER WDx"
End With
''''''''''BELOW IS USED FOR ADD'L ROW FIELDS
'With ActiveSheet.PivotTables(PName).PivotFields("PREPARER NAME")
'.Orientation = xlRowField
'.Position = 2
'End With
'Insert Column Fields
With ActiveSheet.PivotTables(PName).PivotFields("Region")
.Orientation = xlColumnField
.Position = 1
End With
'Insert Data Field
With ActiveSheet.PivotTables(PName).PivotFields("ACCOUNT ID")
.Orientation = xlDataField
.Position = 1
.Function = xlCount
.NumberFormat = "#,##0"
.Name = "Soft Deadlines"
End With
'Filter Page Fields
With ActiveSheet.PivotTables(PName).PivotFields("Certification Status")
.PivotItems("Auto-Certified").Visible = False
.PivotItems("Not Required").Visible = False
End With
End Sub