+ Reply to Thread
Results 1 to 8 of 8

Add and Remove the Fields in an existent Pivot Table through VBA

  1. #1
    Registered User
    Join Date
    11-28-2011
    Location
    Barcelona
    MS-Off Ver
    Excel 2007
    Posts
    74

    Add and Remove the Fields in an existent Pivot Table through VBA

    Hello,

    I am trying to create a system consisting of:
    • A dropdown menu
    • A pivot table
    • A chart

    The idea is that selecting a field from a Dropdown menu, I change the fields in the pivot table (so it changes its display, let's say from having "clients" in the row labels to "locations") and then I get the data from a certain row of the pivot table to create a chart.

    I know how to create pivot tables through VBA, and I know how to refresh them, but I don't know how to control the fields I wish to show in the pivot table. I am sure there might be a way.

    I know that I can add fields with this instruction:

    Please Login or Register  to view this content.
    So I suppose that "Line of Business" is above "model" in the "Row Labels" box, and the same would be with column labels.

    To change the fields, I used the macro recorder and obtained this code:

    Please Login or Register  to view this content.
    The problem is that I would like to know the fields I have in the pivot table in order to always keep control of what I have in each part. If this isn't possible, maybe I can just empti the rows/columns field and re-fill them with the fields I want.

    I would much appreciate if someone could lend me a hand in this issue.
    Last edited by RagonichaFulva; 02-24-2012 at 10:16 AM.

  2. #2
    Registered User
    Join Date
    11-28-2011
    Location
    Barcelona
    MS-Off Ver
    Excel 2007
    Posts
    74

    Re: Add and Remove the Fields in an existent Pivot Table through VBA

    I have seen that maybe it would be better if I worked with a pivot chart (it includes already a Pivot table associated) so I should only control the fields of the chart/pivot table (both are the same entity).

    I'll keep investigating this line of work.

  3. #3
    Registered User
    Join Date
    11-28-2011
    Location
    Barcelona
    MS-Off Ver
    Excel 2007
    Posts
    74

    Re: Add and Remove the Fields in an existent Pivot Table through VBA

    Quote Originally Posted by OnErrorGoto0 View Post
    When you use AddFields, the existing fields should be replaced (unless you specify True for the last argument).

    Hello OnErrorGoto0,

    That's great then.

    What I am thinking is to modify the pivot table associated to my pivot chart, but I wonder how I can apply filters. Which command/method should I use?

    I would try to do:

    a.) Filters: Program
    b.) Rows: Quarter (year)
    c.) Columns: Operation type

    Quite a simple pivot table... I would use AddFields for b.) and c.), but how can I specify the filter and which element of the filter to use?

    Thanks for your assistance.

  4. #4
    Registered User
    Join Date
    11-28-2011
    Location
    Barcelona
    MS-Off Ver
    Excel 2007
    Posts
    74

    Re: Add and Remove the Fields in an existent Pivot Table through VBA

    I found the following code:

    Please Login or Register  to view this content.
    But it seems a little inefficient.

    Is there a way of modifying directly the filter?

  5. #5
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Add and Remove the Fields in an existent Pivot Table through VBA

    If you are using a report filter, then for one item you would simply assign the CurrentPage property. For multiple items I think you will need the loop.
    Good luck.

  6. #6
    Registered User
    Join Date
    11-28-2011
    Location
    Barcelona
    MS-Off Ver
    Excel 2007
    Posts
    74

    Re: Add and Remove the Fields in an existent Pivot Table through VBA

    Quote Originally Posted by OnErrorGoto0 View Post
    If you are using a report filter, then for one item you would simply assign the CurrentPage property. For multiple items I think you will need the loop.
    Hello OnErrorGoto0,

    I haven't found the Currentpage property. For the time being I think I'll use the loop option.

    Thanks for your feedback.

  7. #7
    Forum Expert OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,655

    Re: Add and Remove the Fields in an existent Pivot Table through VBA

    CurrentPage is a property of the pivotfield object.
    Please Login or Register  to view this content.
    for example.
    Last edited by OnErrorGoto0; 02-24-2012 at 10:19 AM.

  8. #8
    Registered User
    Join Date
    11-28-2011
    Location
    Barcelona
    MS-Off Ver
    Excel 2007
    Posts
    74

    Re: Add and Remove the Fields in an existent Pivot Table through VBA

    Oh, I see.

    Thanks for the info.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1