Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 4th, 2003, 09:05 PM
Authorized User
 
Join Date: Sep 2003
Location: Escalon, CA, USA.
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


Reply With Quote
  #2 (permalink)  
Old November 5th, 2003, 03:54 PM
Authorized User
 
Join Date: Jun 2003
Location: , , .
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.
Reply With Quote
  #3 (permalink)  
Old August 19th, 2005, 06:06 AM
Registered User
 
Join Date: Nov 2004
Location: , , .
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.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


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



All times are GMT -4. The time now is 09:37 AM.


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