I have the following procedure for creating a pivot table. Normally it works fine and produces a "Sum of balance" in the data area.
However, every now and then, on data in a new spreadsheet, it does a "Count" of the data instead of the "Sum"
Why does it do this and how can I ensure that it only does a Sum?
I would appreciate help on this.
Regards, JAKS999
Sub CreatePivotTable()
Dim PTcache As PivotCache
Dim PT As PivotTable
Range("A5").Select
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("A5").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:="BudgetPivot")
With PT
' Add fields
.PivotFields("Year").Orientation = xlColumnField
.PivotFields("Activity").Orientation = xlRowField
.PivotFields("Type").Orientation = xlRowField
.PivotFields("Balance").Orientation = xlDataField
End With
ActiveSheet.PivotTables("BudgetPivot"). _
PivotFields("Sum of Balance").Function = xlSum
Columns("C:E").Select
Selection.Style = "Comma"
Columns("C:E").EntireColumn.AutoFit
Columns("A:A").EntireColumn.AutoFit
Application.ScreenUpdating = True
On Error Resume Next
Sheets("Sheet1").DrawingObjects("TextBoxWait").Visible = False
End Sub
However, every now and then, on data in a new spreadsheet, it does a "Count" of the data instead of the "Sum"
Why does it do this and how can I ensure that it only does a Sum?
I would appreciate help on this.
Regards, JAKS999
Sub CreatePivotTable()
Dim PTcache As PivotCache
Dim PT As PivotTable
Range("A5").Select
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("A5").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:="BudgetPivot")
With PT
' Add fields
.PivotFields("Year").Orientation = xlColumnField
.PivotFields("Activity").Orientation = xlRowField
.PivotFields("Type").Orientation = xlRowField
.PivotFields("Balance").Orientation = xlDataField
End With
ActiveSheet.PivotTables("BudgetPivot"). _
PivotFields("Sum of Balance").Function = xlSum
Columns("C:E").Select
Selection.Style = "Comma"
Columns("C:E").EntireColumn.AutoFit
Columns("A:A").EntireColumn.AutoFit
Application.ScreenUpdating = True
On Error Resume Next
Sheets("Sheet1").DrawingObjects("TextBoxWait").Visible = False
End Sub