PDA

View Full Version : Problem with Calculated fields in the Pivot Table



vbpivot
01-16-2007, 03:29 PM
I have this pivot table that I wanted to add Calculated fields. Everything works, but I would like to get the zero or blank entries out. Including code

SHIPTOPLANTJANOCT P/DOCTNOV P/DNOVDEC P/DDECJAN P/DGrand TotalFREIGHTLINERBLUE DIAMOND 0 0 0 00 CHATHAM 00000 CHILLICOTHE 00000 CLEVELAND113896.590909092125121.65243384.894736841613126.44444447738.58009 DENTON 00000 DUBLIN 00000 ESCOBEDO 00000 GARLAND 00000 MACUNGIE 00000 MADISON 00000 MOUNT HOLLY155.40909090911911.12226.7894736841291.666666667509.9652313 PORTLAND24121.5454545547430.260419.2631578936626.777777781782.78639 RENTON 00000 SANTIAGO27227.6818181860938.777420.5263157939030.222222222162.130356 ST. THOMAS20211.525319.553918.52631578916222.444444441070.02076 URBANA 00000 WINDSOR 00000FREIGHTLINER Total1868162.72727273580221.244241402660207.555555613263.48283

Sub CreatePivotTable()
Dim PTcache As PivotCache
Dim PT As PivotTable

On Error Resume Next
Sheets("Sheet1").DrawingObjects("TextBoxWait").Visible = True
On Error GoTo 0
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.Add( _
SourceType:=xlDatabase, _
SourceData:=Range("A1").CurrentRegion.Address)

' Add new worksheet
Worksheets.Add
ActiveSheet.Name = "PivotSheet"
' Create the Pivot Table from the Cache
Set PT = PTcache.CreatePivotTable( _
TableDestination:=Sheets("PivotSheet").Range("A1"), _
TableName:="OEMShipPivot")

With PT
' Add fields
.PivotFields("SHIPTO").Orientation = xlRowField
.PivotFields("PLANT").Orientation = xlRowField
.PivotFields("SFYYMM").Orientation = xlColumnField
.PivotFields("SHIPQTY").Orientation = xlDataField

' Add calculated items
.PivotFields("SFYYMM").CalculatedItems.Add "OCT P/D", "= OCT/22"
.PivotFields("SFYYMM").CalculatedItems.Add "NOV P/D", "= NOV/20"
.PivotFields("SFYYMM").CalculatedItems.Add "DEC P/D", "= DEC/19"
.PivotFields("SFYYMM").CalculatedItems.Add "JAN P/D", "= JAN/9"

' Move the calculated items
.PivotFields("SFYYMM").PivotItems("OCT P/D").Position = 2
.PivotFields("SFYYMM").PivotItems("NOV P/D").Position = 4
.PivotFields("SFYYMM").PivotItems("DEC P/D").Position = 6
.PivotFields("SFYYMM").PivotItems("JAN P/D").Position = 8


End With
Application.ScreenUpdating = True
On Error Resume Next
Sheets("Sheet1").DrawingObjects("TextBoxWait").Visible = False
End Sub

matthewspatrick
01-16-2007, 07:05 PM
Welcome to VBAX!

Two things:

Please use the VBA tags; they make your code much easier to read
Please attach a sample file