Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Pivot Table - Add calculated field using field number (index) vs n

I'm building out some financial reports where the titles change every month -
consequently changes my calculated field refrences. The sequence or position
of the fields doesn't change & the ideal solution for me would be to
reference the pivot fields with an index number. I've made several attempts
and done quite a bit of searching with no luck. Working with Excel 2007. My
code is below and the code that doesn't work is inside the With block.
Basically, I'm creating the Pivot Table using VBA - inside the with block not
displayed I'm adding Page Fields, Row Fields and quite a few Data Fields.

Dim PTCache As PivotCache
Dim PT As PivotTable
Application.Calculation = xlManual

Set PTCache = ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatab ase,
SourceData:="PVTData")

Set PT = PTCache.CreatePivotTable(TableDestination:=Sheet1. Range("A21"),
TableName:="PivotTable1")

With PT

ActiveSheet.PivotTables("PivotTable1").CalculatedF ields.Add
"BudgetYTD", "=" & PT.PivotFields(71).Name & "+" & PT.PivotFields(72).Name &
"+" & _
PT.PivotFields(73).Name & "+" & PT.PivotFields(74).Name & "+" &
PT.PivotFields(75).Name & "+" & PT.PivotFields(76).Name & "+" & _
PT.PivotFields(77).Name & "+" & PT.PivotFields(78).Name & "+" &
PT.PivotFields(79).Name & "+" & PT.PivotFields(80).Name & "+" & _
PT.PivotFields(81).Name & "+" & PT.PivotFields(82).Name, True

ActiveSheet.PivotTables("PivotTable1").AddDataFiel d
ActiveSheet.PivotTables("PivotTable1").PivotFields ("BudgetYTD"), "Sum of
BudgetYTD", xlSum

End With
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I remove a Calculated Field from the Pivot Table field list TheTraveler Excel Discussion (Misc queries) 2 April 9th 10 06:55 PM
Pivot table, IF function, calculated item versus calculated field NomadPurple Excel Discussion (Misc queries) 1 March 9th 10 03:17 PM
Creating a Calculated Field in a Pivot Table for same field dza7 Excel Discussion (Misc queries) 3 October 21st 09 11:22 PM
Creating a Calculated Field in a Pivot Table for same field dza7 Excel Programming 3 October 21st 09 11:22 PM
Pivot Table Formulas Calculated Item / Calculated Field Vikram Dhemare Excel Programming 2 October 10th 06 08:45 AM


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

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"