Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using functions in Pivot Tables

Status
Not open for further replies.

cfcanter

Technical User
Sep 12, 2001
31
US
I am trying to create a pivot table using xl 2002. The following code words using the macro recorder:

With ActiveSheet.PivotTables("PivotTable2").PivotFields("STD COST")
.Orientation = xlDataField
.Caption = "Max of STD COST"
.Function = xlMax
End With

However, the code is cumbersome and pivot tables can't do the calculated fields I need.

This code should do the same thing but doesn't work:

'this creates the pivot table
Set ptcache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:=Range("a1").CurrentRegion.Address)
'
'create the pivot table from the cache
'
Set pt = ptcache.CreatePivotTable(Sheets("bypart").Range("a1"), "ByPartNumber")
With pt
'add row fields
.PivotFields("Part Number").Orientation = xlRowField
.PivotFields("Description").Orientation = xlRowField
' add calculated fields
.CalculatedFields.Add "Sales $ per Unit", "=('Total Sales'/'Sales Qty')"
.CalculatedFields.Add "Total Std Cost", "=('Std Cost' * 'Sales Qty')"
.CalculatedFields.Add "Total GM $", "=('Total Sales' - 'Total Std Cost')"
.CalculatedFields.Add "GM $ per Unit", "=('Sales $ per Unit' - 'Std Cost')"
' places result data in pivot tables
.PivotFields("Sales Qty").Orientation = xlDataField
.PivotFields("Total Sales").Orientation = xlDataField
.PivotFields("Sales $ per Unit").Orientation = xlDataField
.PivotFields("Total Std Cost").Orientation = xlDataField
.PivotFields("Std Cost").Orientation = xlDataField
.PivotFields("Std Cost").Caption = "STD COST"

' fails here
.PivotFields("Std Cost").Function = xlMax

Why isn't the function working? How can I fix it?

Craig Canter
Cost Accountant
Sipex
Milpitas, CA
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top