peltier tech utilities
Learn how to create Excel dashboards.

Categories

30 Excel Functions in 30 Days

Archives

Remove Pivot Table Calculated Field With Excel VBA

Yesterday, I started out with the best of intentions, planning to get some work done, and find a couple of topics for upcoming blog posts. Then, while sipping my morning coffee and reading the RSS feeds, I clicked on an article about pivot tables. There were a few lines of sample code for creating a list of fields in the pivot table, which seemed like a good idea, but needed improvement. And there went the day!


But, in the end, I had some useful code for removing calculated fields from the pivot table layout, so the day was productive, but not in the way that I anticipated.


Strange Results for Orientation Property


The journey started out with that simple list of pivot table field names. I wanted to see more information about each field, so I wrote some Excel VBA code to list the pivot field caption, source name, location (orientation), position, sample data, and a formula (calculated fields).


Everything worked well, but the Orientation property gave some strange results for the fields in the Values area. Instead of listing them as Data, they were shown as Hidden.


PivotFieldListCode01


Check the Data Fields


Next, I tried a different approach, looping through each type of pivot field separately, e.g. RowFields, DataFields. In that list, the orientation was correct, but no sample items were shown for the data fields, and the calculated field formulas weren't listed.


PivotFieldListCode02


Removing the Data Fields


Going back to the first code example, I played with the Orientation property in the code, but had no success in getting it to display correctly when looping through the PivotFields. When I tried to change the Orientation for the data fields, things got even stranger. I could change the regular data fields to xlHidden, to remove them from the pivot table layout, but those calculated fields wouldn't budge. 


Instead of changing the calculated field Orientation, Excel displayed the error message "Run-time error '1004': Unable to set the Orientation property of the PivotField class"


PivotFieldListCode03


You can manually uncheck the calculated field boxes, and remove them from the pivot table, then check the box again, to put it back into the layout. However, if you record code while removing the calculated field, that recorded code shows the same error message when you try to run it.


Remove the Calculated Fields


A Google search showed that many other people had encountered this problem, and the only solution seemed to be to delete the calculated field, instead of trying to remove it from the layout. The Google search turned up a line of code to delete the offending calculated field, but that wouldn't be much help if you wanted to keep the calculated field in your pivot table, for use later.


So, I wrote the following code that deletes each calculated field, then immediately adds it back to the pivot table field list, but not into the pivot table layout. If you've been having the same trouble with calculated fields, I hope this helps!

Sub RemoveCalculatedFields()
Dim pt As PivotTable
Dim pf As PivotField
Dim pfNew As PivotField
Dim strSource As String
Dim strFormula As String

Set pt = ActiveSheet.PivotTables(1)
For Each pf In pt.CalculatedFields
strSource = pf.SourceName
strFormula = pf.Formula
pf.Delete
Set pfNew = pt.CalculatedFields.Add(strSource, strFormula)
Next pf

End Sub


________________

Related Posts Plugin for WordPress, Blogger...

9 comments to Remove Pivot Table Calculated Field With Excel VBA

  • Jeff Weir

    Hi Debbra. Got a question related to your blog in general rather than this post specifically, which is: Do you have a comment feed for all comments that I can subscribe to? I enjoy the insights I pick up from the daily dose of excel comment feed (as well as the blog proper) and would like to be able to glean the same insights from your blog if possible.

    Regards

    jeff

  • Hi Jeff, thanks for asking about the comments feed. I've added a link at the bottom of the right sidebar.
    The link is: http://feeds.feedburner.com/ContexturesBlogComments

    Debra

  • Awesome ! Was stuck in it for last 10 minutes and you saved my next 50 minutes :) Thanks again :)

  • Erwan de Kerg

    Hi Debra,

    Topics really interesting. I experimented your code RemoveCalculatedFields() ; its OK in Excel 2007, but NOT in Excel 2003 SP3.

    my problem is that, using Excel 2003, I want to remove all the CalculatedField from the DataField zone in my PivotTable with about 12 CalculatedField and 3 DataField.
    I tried the Orientation property without success (as you explained it in your blog)

    As i want to remove all the 12 CalculatedFields and keep the 3 others, I tried then your code :
    For Each pf In pt.CalculatedFields
    pf.Delete
    Next pf

    but it doesn't work in my Excel 2003 SP3.

    Any ideas ?

    many thanx

  • @Erwan, the code works for me in Excel 2003 SP3. What happened when you tried it? What didn't work?

  • Ryan

    Thank you so much... this worked perfectly. I was about to lose it when the Caculated field would not hide.

  • Jeff C

    Thank you! Thank you! Thank you!

    I've been hampered by this for much longer than I care to admit. After spending all morning googling I finally found your solution.

    Great work!

  • Villa

    Excelent work, i spent a lot of time into this, thank you so much.

  • David

    My calculated field (1) depends from another calculated field (2) so when i use this macro, my formula(1) colapse and it delete the reference from my other calculated field (2).

Leave a Reply

  

  

  

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>