Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 4th, 2003, 09:05 PM
Authorized User
 
Join Date: Sep 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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


 
Old November 5th, 2003, 03:54 PM
Authorized User
 
Join Date: Jun 2003
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.
 
Old August 19th, 2005, 06:06 AM
Registered User
 
Join Date: Nov 2004
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Renaming fields in access table using VBA imaley Access VBA 13 August 13th, 2009 02:22 PM
Pivot Table Dynamic Reference Data oyelesit Excel VBA 2 August 4th, 2006 07:28 AM
Pivot Table Fields Function gastoncs Excel VBA 3 January 16th, 2006 07:04 AM
Help Needed to write vba for Pivot Table in Excel sunny76 Excel VBA 1 June 28th, 2005 01:44 AM
Getting the data in columns - like Pivot Table merguvan Access VBA 4 February 29th, 2004 02:31 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.