ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Pivot Table Problem (https://www.excelbanter.com/excel-programming/283313-pivot-table-problem.html)

Boyd L. Colglazier

Pivot Table Problem
 

I am using the following code to create a pivot table which I want to return
the sum function for "Balance". For some reason, the pivot table started
returning Count of Balance rather than Sum of Balance so I added the line
..PivotFields("Count of Balance").Function=xlSum. This changed the count to
sum. Now the pivot table is once again returning Sum of Balance initially
and the code hangs when it trys to convert the count to sum. Is there some
code I can use to cause the function to default to sum each time the pivot
table runs?

Range(FirstCell, LastCell).Select
CurrentRegion = Selection

Set PTCache = ActiveWorkbook.PivotCaches.Add _
(SourceType:=xlDatabase, _
SourceData:=Range("A1").CurrentRegion.Address)

'Balance by AssignedTo
Set PT = PTCache.CreatePivotTable _
(TableDestination:="'AssignedTo'!R3C1", _
TableName:="PivotTable1")
With PT
.PivotFields("AssignedTo").Orientation = xlRowField
.PivotFields("Balance").Orientation = xlDataField
.PivotFields("Count of Balance").Function = xlSum
End With



Debra Dalgleish

Pivot Table Problem
 
You could change your code slightly:

'===========================
With PT.PivotFields("Balance")
.Orientation = xlDataField
.Caption = "Balance "
.Function = xlSum
End With
'============================

Boyd L. Colglazier wrote:
I am using the following code to create a pivot table which I want to return
the sum function for "Balance". For some reason, the pivot table started
returning Count of Balance rather than Sum of Balance so I added the line
.PivotFields("Count of Balance").Function=xlSum. This changed the count to
sum. Now the pivot table is once again returning Sum of Balance initially
and the code hangs when it trys to convert the count to sum. Is there some
code I can use to cause the function to default to sum each time the pivot
table runs?

Range(FirstCell, LastCell).Select
CurrentRegion = Selection

Set PTCache = ActiveWorkbook.PivotCaches.Add _
(SourceType:=xlDatabase, _
SourceData:=Range("A1").CurrentRegion.Address)

'Balance by AssignedTo
Set PT = PTCache.CreatePivotTable _
(TableDestination:="'AssignedTo'!R3C1", _
TableName:="PivotTable1")
With PT
.PivotFields("AssignedTo").Orientation = xlRowField
.PivotFields("Balance").Orientation = xlDataField
.PivotFields("Count of Balance").Function = xlSum
End With




--
Debra Dalgleish
Excel FAQ, Tips & Book List
http://www.contextures.com/tiptech.html



All times are GMT +1. The time now is 12:29 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com