+ Reply to Thread
Results 1 to 9 of 9

VBA Code for Removing All Value Fields in a Pivot Table

  1. #1
    Registered User
    Join Date
    11-05-2009
    Location
    York, England
    MS-Off Ver
    Excel 2003
    Posts
    4

    VBA Code for Removing All Value Fields in a Pivot Table

    Hi all,

    New to the forum and had a quick look through the search and couldnt find this query already here.

    Basically I'm looking for VBA code to remove all of the Value Fields present in a PivotTable. I dont want it to be Field specific such as the below code that I'm currently using:

    Please Login or Register  to view this content.
    More like something similar to this: (But for Fields instead of Filters!)

    Please Login or Register  to view this content.
    I have attached an example to show the starting point and the desired result.

    Any help would be appreciated.

    Thanks!


    Please see below:
    Attached Files Attached Files
    Last edited by DonkeyOte; 11-05-2009 at 11:20 AM.

  2. #2
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: VBA Code for Removing All Value Fields in a Pivot Table

    By value fields I presume you mean Data Fields ... at which point something along the lines of the below perhaps ?

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    11-05-2009
    Location
    York, England
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: VBA Code for Removing All Value Fields in a Pivot Table

    Thanks!

    That works apart from on Calculated Fields I think?!

    Can I manipulate the code to remove these as well?

  4. #4
    Registered User
    Join Date
    11-05-2009
    Location
    York, England
    MS-Off Ver
    Excel 2003
    Posts
    4

    Re: VBA Code for Removing All Value Fields in a Pivot Table

    Thanks!

    That works apart from on Calculated Fields I think?!

    Can I manipulate the code to remove these as well?
    Attached Files Attached Files

  5. #5
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: VBA Code for Removing All Value Fields in a Pivot Table

    Yes, though you need to adapt the code slightly...

    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    11-07-2009
    Location
    India
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: VBA Code for Removing All Value Fields in a Pivot Table

    please send me who to import data from yahoo finance stock name last trade price day high day low

  7. #7
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: VBA Code for Removing All Value Fields in a Pivot Table

    m1806,

    Your post does not comply with Rule 2 of our Forum RULES. Don't post a question in the thread of another member -- start your own thread.

  8. #8
    Registered User
    Join Date
    11-22-2012
    Location
    Karlsruhe, Germany
    MS-Off Ver
    Excel 2007
    Posts
    9

    Re: VBA Code for Removing All Value Fields in a Pivot Table

    Dim PT As PivotTable, ptField As PivotField
    Set PT = ActiveSheet.PivotTables("PivotTable2")
    For Each ptField In PT.DataFields
    ptField.Orientation = xlHidden
    Next ptField
    Set PT = Nothing
    Donkey, regarding the same doubt of the colleague, is it also possible to add a condition, like if I want to keep one or two data fields and remove the others?

    Thanks in advance

  9. #9
    Registered User
    Join Date
    01-23-2015
    Location
    Tychy, Poland
    MS-Off Ver
    2010
    Posts
    1

    Re: VBA Code for Removing All Value Fields in a Pivot Table

    sorry for being late for a dinner, but maybe someone find it useful. My approach is to iterate through all column names which I use for a pivot and by suspending errors it hides all of the fields I use as a row in pivot:

    On Error Resume Next
    For i = 1 To ActiveSheet.PivotTables("YTD_RevRec").PivotFields.Count
    ActiveSheet.PivotTables("YTD_RevRec").PivotFields(i).Orientation = xlHidden
    Next
    On Error GoTo 0
    so if you want to keep any for the rows just add there an if statement, something like this:
    if .PivotFields.name = "whatever" then...

+ 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