Hi
I'm trying to create a pivot table with a calculated field using the following code. When I get to the last line, I get the mesage "Unable to get the PivotFields property of the PivotTable Class." I'd really appreciate any help in figuring out why. Thank you very much.
DaveH1
Sub Main()
Dim PTcache As PivotCache
Dim pt As PivotTable
Application.ScreenUpdating = False
' Delete PivotSheet if it exists
On Error Resume Next
Application.DisplayAlerts = False
Sheets("PivotSheet").Delete
On Error GoTo 0
' Create a Pivot Cache
Set PTcache = ActiveWorkbook.PivotCaches.Create( _
SourceType:=xlDatabase, _
SourceData:=Range("A1").CurrentRegion.Address)
' Add new worksheet
Worksheets.Add
ActiveSheet.Name = "PivotSheet"
ActiveWindow.DisplayGridlines = False
' Create the Pivot Table from the Cache
Set pt = ActiveSheet.PivotTables.Add( _
PivotCache:=PTcache, _
TableDestination:=Range("A1"), _
TableName:="EngagementPivot")
With pt
' Add fields for filtering
.PivotFields("PhoneAvailable").Orientation = xlPageField
.PivotFields("CanCall?").Orientation = xlPageField
.PivotFields("Referred?").Orientation = xlPageField
.PivotFields("Month&Year_Expected_Start").Orientation = xlPageField
.PivotFields("Month&Year_Actual_Start").Orientation = xlPageField
.PivotFields("DeliveryConsultant").Orientation = xlPageField
.PivotFields("SalesConsultant").Orientation = xlPageField
.PivotFields("Scheduler").Orientation = xlPageField
.PivotFields("ProgramFeeRange").Orientation = xlPageField
' Add fields for rows
.PivotFields("OriginatingFirm").Orientation = xlRowField
.PivotFields("DeliveringFirm").Orientation = xlRowField
' Add fields for non-calculated values
.PivotFields("ProgramFee").Orientation = xlDataField
.PivotFields("#Engaged").Orientation = xlDataField
.PivotFields("$Engaged").Orientation = xlDataField
.PivotFields("#atRisk").Orientation = xlDataField
.PivotFields("$atRisk").Orientation = xlDataField
' Add a calculated field to compute percent engagement
.CalculatedFields.Add "Engaged_%", "=$Engaged/ProgramFee"
.PivotFields("Engaged_%").Orientation = xlDataField
I'm trying to create a pivot table with a calculated field using the following code. When I get to the last line, I get the mesage "Unable to get the PivotFields property of the PivotTable Class." I'd really appreciate any help in figuring out why. Thank you very much.
DaveH1
Sub Main()
Dim PTcache As PivotCache
Dim pt As PivotTable
Application.ScreenUpdating = False
' Delete PivotSheet if it exists
On Error Resume Next
Application.DisplayAlerts = False
Sheets("PivotSheet").Delete
On Error GoTo 0
' Create a Pivot Cache
Set PTcache = ActiveWorkbook.PivotCaches.Create( _
SourceType:=xlDatabase, _
SourceData:=Range("A1").CurrentRegion.Address)
' Add new worksheet
Worksheets.Add
ActiveSheet.Name = "PivotSheet"
ActiveWindow.DisplayGridlines = False
' Create the Pivot Table from the Cache
Set pt = ActiveSheet.PivotTables.Add( _
PivotCache:=PTcache, _
TableDestination:=Range("A1"), _
TableName:="EngagementPivot")
With pt
' Add fields for filtering
.PivotFields("PhoneAvailable").Orientation = xlPageField
.PivotFields("CanCall?").Orientation = xlPageField
.PivotFields("Referred?").Orientation = xlPageField
.PivotFields("Month&Year_Expected_Start").Orientation = xlPageField
.PivotFields("Month&Year_Actual_Start").Orientation = xlPageField
.PivotFields("DeliveryConsultant").Orientation = xlPageField
.PivotFields("SalesConsultant").Orientation = xlPageField
.PivotFields("Scheduler").Orientation = xlPageField
.PivotFields("ProgramFeeRange").Orientation = xlPageField
' Add fields for rows
.PivotFields("OriginatingFirm").Orientation = xlRowField
.PivotFields("DeliveringFirm").Orientation = xlRowField
' Add fields for non-calculated values
.PivotFields("ProgramFee").Orientation = xlDataField
.PivotFields("#Engaged").Orientation = xlDataField
.PivotFields("$Engaged").Orientation = xlDataField
.PivotFields("#atRisk").Orientation = xlDataField
.PivotFields("$atRisk").Orientation = xlDataField
' Add a calculated field to compute percent engagement
.CalculatedFields.Add "Engaged_%", "=$Engaged/ProgramFee"
.PivotFields("Engaged_%").Orientation = xlDataField