+ Reply to Thread
Results 1 to 3 of 3

Apply code in all pivot tables in the same worksheet

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    02-07-2013
    Location
    Philippines
    MS-Off Ver
    Excel Online; 365
    Posts
    610

    Apply code in all pivot tables in the same worksheet

    Hi Experts,

    i found this code but I could not modify it so it would work on two pivot tables on the same sheet:

    Set TargetDate = Range("A1")
    Dim PI As PivotItem


    For Each PI In ActiveSheet.PivotTables("PivotTable1").PivotFields("Target Due Date").PivotItems
    If DateValue(PI.Name) < TargetDate Or DateValue(PI.Name) = TargetDate Then
    PI.Visible = True
    Else
    PI.Visible = False
    End If
    Next PI

    How do I get a code to work on at least two pivot tables on the same sheet?

    I apologize if i do not know how to use code tags.

    Thank you
    If I've helped U pls click on d *Add Reputation

  2. #2
    Forum Expert dflak's Avatar
    Join Date
    11-24-2015
    Location
    North Carolina
    MS-Off Ver
    365
    Posts
    7,926

    Re: Apply code in all pivot tables in the same worksheet

    You're missing a thing or two. There are two ways to point to a lot of things in VB. One is by what I like to call "array" like Sheet(1) or Sheet("SheetName"). Normally you address these items like:
    For I = 1 to 10
        Sheets(I). dosomething
    next
    The other is by pointer or "object" which is what I will use here. These are addressed using For Each
    For Each sh in ActiveWorkbook.Sheets
    This should work for your purposes.
    Public Sub Update_Pivot()
    Dim sh As Worksheet         ' Worksheet containing the pivot table
    Dim pt As PivotTable        ' Pivot table "object"
    Dim pf As PivotField        ' Pivot field "object"
    Dim pi As PivotItem         ' Pivot item "object"
    
    ' Set the sheet
    Set sh = Sheets("YourSheetName")
    
    ' Loop through each pivot table on the sheet
    For Each pt In sh.PivotTables
        ' Set the pivot field
        Set pf = pt.PivotFields("Target Due Date")
        
        ' You can't have all the pivot items false, so you have to turn on all of them
        ' then turn off the ones you don't use
        For Each pi In pf.PivotItems
            pi.Visible = True
        Next pi
        
        For Each pi In pf.PivotItems
            If DateValue(pi.Value) <= TargetDate Then
                pi.Visible = True
            Else
                pi.Visible = False
            End If
        Next pi
    Next pt
    
    End Sub
    One spreadsheet to rule them all. One spreadsheet to find them. One spreadsheet to bring them all and at corporate, bind them.

    A picture is worth a thousand words, but a sample spreadsheet is more likely to be worked on.

  3. #3
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,169

    Re: Apply code in all pivot tables in the same worksheet

    Hi,

    Try changing
    For Each PI In ActiveSheet.PivotTables("PivotTable1").PivotFields("Target Due Date").PivotItems
    to

    Dim PTCtr as double
    For PtCtr = 1 to Activesheet.PivotTables.Count
    PI In ActiveSheet.PivotTables(PTCtr).PivotFields("Target Due Date").PivotItems
    'your other code
    Next PTCtr
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Copy worksheet and VBA code to only apply to new worksheet
    By tejay in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-04-2015, 09:45 PM
  2. [SOLVED] Pivot Tables - How do you apply a new colour to one of the preset styles
    By MissDB in forum Excel Charting & Pivots
    Replies: 6
    Last Post: 10-06-2014, 12:40 PM
  3. Apply a procedure only to specific pivot tables (MDX)
    By nomad80 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-19-2014, 07:24 AM
  4. [SOLVED] VBA code to launch macro on another worksheet and generate pivot tables
    By greyscale in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-22-2012, 01:14 AM
  5. Replies: 0
    Last Post: 09-22-2012, 07:22 PM
  6. Replies: 1
    Last Post: 02-22-2012, 12:21 PM
  7. [SOLVED] How does the term 'pivot' apply to Excel's Pivot tables and Pivot.
    By stvermont in forum Excel General
    Replies: 1
    Last Post: 02-18-2005, 10:06 AM

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