Pivot Items | Using Pivot Items In VBA

mreman81

New Member
Joined
Oct 14, 2010
Messages
43
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.



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
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

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