p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Excel VBA (http://p2p.wrox.com/forumdisplay.php?f=79)
-   -   How to replace pivot table data fields using VBA? (http://p2p.wrox.com/showthread.php?t=4587)

rstober October 4th, 2003 09:05 PM

How to replace pivot table data fields using VBA?
 
Hi,

I'm progamming a pivot table using VBA. I have no problem manipulating
column or row fields, but how can I replace a data field? Everything I try just adds the data field to the existing data field resulting in a mess.

The PivotTable.addfields methods *replaces* whatever columns were previously designated as RowFields, ColumnFields, and PageFields with the new specifications, as in:

.AddFields RowFields:="Date", PageFields:="Location"

But no matter what I try, I can't replace the data field.... I've even tried setting the Orientation property to xlHidden, as follows:

' Hide existing data fields
For each pf In .DataFields
    Debug.Print "deleting pf", pf.Name
    pf.Orientation = xlHidden
    'pf.Delete ' I tried deleting too
Next

They key is that I need to do this via code. I can easily do it by using the Excel GUI. I recorded the resulting code - Excel was hidding the field by setting its Orientation property to xlHidden. Why doesn't that work for me?

Many thanks in advance,

Robert Stober



taliesin November 5th, 2003 03:54 PM

Was looking thru this forum for something else (UserForm question) and saw this old topic. Don't know if you'll check in, but I've written a VBA program to generate several different PivotTable layouts, copy each table layout to a new worksheet and format the data nicely for our Sales staff.

I believe your first problem is that the .DataFields property returns a read-only (according to the Help file for the DataFields property) collection of fields that have their .Orientation property set to xlDataField.

What you want to do is access the appropriate PivotField that happens to be currently assigned as an xlDataField and change it to xlHidden.

I do not change the data fields in my app, but here is my code to setup one of my data fields:

    With ActiveSheet.PivotTables("DataPivot").PivotFields(T HIS_FISCAL_MONTH)
        .Orientation = xlDataField
        .Caption = THIS_FISCAL_CAPTION
        .Position = 1
        .Function = xlSum
    End With


The constant THIS_FISCAL_MONTH is the column name in the worksheet that I am making into a data field, the other constant is the displayed data field caption. Using the constants allows me to easily change these parameters every month for the new report.

So, if I wanted to hide a data field I would change my code in the following manner:

    With ActiveSheet.PivotTables("DataPivot").PivotFields(T HIS_FISCAL_MONTH)
        .Orientation = xlHidden
    End With

or much simplified (and not fully qualified):
.PivotFields(THIS_FISCAL_MONTH).Orientation = xlHidden

I believe this will do the trick for you.

sanjna000 August 19th, 2005 06:06 AM

Quote:

quote:Originally posted by taliesin
 Was looking thru this forum for something else (UserForm question) and saw this old topic. Don't know if you'll check in, but I've written a VBA program to generate several different PivotTable layouts, copy each table layout to a new worksheet and format the data nicely for our Sales staff.

I believe your first problem is that the .DataFields property returns a read-only (according to the Help file for the DataFields property) collection of fields that have their .Orientation property set to xlDataField.

What you want to do is access the appropriate PivotField that happens to be currently assigned as an xlDataField and change it to xlHidden.

I do not change the data fields in my app, but here is my code to setup one of my data fields:

    With ActiveSheet.PivotTables("DataPivot").PivotFields(T HIS_FISCAL_MONTH)
        .Orientation = xlDataField
        .Caption = THIS_FISCAL_CAPTION
        .Position = 1
        .Function = xlSum
    End With


The constant THIS_FISCAL_MONTH is the column name in the worksheet that I am making into a data field, the other constant is the displayed data field caption. Using the constants allows me to easily change these parameters every month for the new report.

So, if I wanted to hide a data field I would change my code in the following manner:

    With ActiveSheet.PivotTables("DataPivot").PivotFields(T HIS_FISCAL_MONTH)
        .Orientation = xlHidden
    End With

or much simplified (and not fully qualified):
.PivotFields(THIS_FISCAL_MONTH).Orientation = xlHidden

I believe this will do the trick for you.


All times are GMT -4. The time now is 03:11 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.