MWhiteDesigns
Well-known Member
- Joined
- Nov 17, 2010
- Messages
- 646
- Office Version
- 2016
- Platform
- Windows
Good morning,
i have the below code to create a simple pivot table. I'd like to take the "Within SLA" field and divide it by the "Grand Total" Field.
I'd like to be able to do this within the pivot fields but I don't know if its possible since its two separate fields?
Thoughts?
i have the below code to create a simple pivot table. I'd like to take the "Within SLA" field and divide it by the "Grand Total" Field.
I'd like to be able to do this within the pivot fields but I don't know if its possible since its two separate fields?
Thoughts?
Code:
Dim PCache As PivotCache, LastRow As Long, pt As PivotTable
'If "Pivot" worksheet already exists, delete it
On Error Resume Next
Application.DisplayAlerts = False
Sheets("Summary").Delete
On Error GoTo 0
Application.DisplayAlerts = True
Worksheets("Data").Activate
Set PCache = ActiveWorkbook.PivotCaches.create(SourceType:=1, SourceData:=Range("A1").CurrentRegion.Address)
Worksheets.Add
ActiveSheet.Name = "Summary"
ActiveWindow.DisplayGridlines = False
Set pt = ActiveSheet.PivotTables.Add(PivotCache:=PCache, TableDestination:=Range("C2"), TableName:="PivotTable1")
'Select fields for PivotTable
ActiveWorkbook.ShowPivotTableFieldList = True
With pt.PivotFields("LOB")
.Orientation = xlRowField
.Position = 1
End With
'Select Columns and Data
With pt.PivotFields("Inside SLA")
.Orientation = xlColumnField
.Position = 1
End With
pt.AddDataField pt.PivotFields("Inside SLA"), "Within SLA", xlCount
With pt.PivotFields("ALL SLA")
.Orientation = xlColumnField
.Position = 1
End With
pt.AddDataField pt.PivotFields("ALL SLA"), "Grand Total", xlCount
ActiveWorkbook.ShowPivotTableFieldList = False