loop through all pivots and do these actions

daveyc18

Well-known Member
Joined
Feb 11, 2013
Messages
741
Office Version
  1. 365
  2. 2010
Code:
  With ActiveSheet.PivotTables("PivotTable54").PivotFields("Date")
        .PivotItems("Aug 21").Visible = True
        .PivotItems("Code").Visible = False
        .PivotItems("Dec 21").Visible = True
        .PivotItems("Nov 21").Visible = True
        .PivotItems("Oct 21").Visible = True
        .PivotItems("Sep 21").Visible = True
        .PivotItems("Jan 22").Visible = True
        .PivotItems("Feb 22").Visible = True
        .PivotItems("Mar 22").Visible = True
        .PivotItems("Apr 22").Visible = True
        .PivotItems("May 22").Visible = True
        .PivotItems("Jun 22").Visible = True
        .PivotItems("Jul 22").Visible = True
        .PivotItems("(blank)").Visible = False
        
    End With

    
    
    
    ActiveSheet.PivotTables("PivotTable54").PivotSelect "'Sep 21'", xlDataAndLabel _
        + xlFirstRow, True
    Range("A325").Select
    ActiveSheet.PivotTables("PivotTable54").PivotFields("Date").PivotItems("Sep 21" _
        ).Position = 1
    Range("A321").Select
    ActiveSheet.PivotTables("PivotTable54").PivotFields("Date").PivotItems("Sep 21" _
        ).Position = 2
    Range("A324").Select
    ActiveSheet.PivotTables("PivotTable54").PivotFields("Date").PivotItems("Code"). _
        Position = 3
    Range("A325").Select
    ActiveSheet.PivotTables("PivotTable54").PivotFields("Date").PivotItems("Oct 21" _
        ).Position = 1
    Range("A321").Select
    ActiveSheet.PivotTables("PivotTable54").PivotFields("Date").PivotItems("Oct 21" _
        ).Position = 2
    Range("A322").Select
  ActiveSheet.PivotTables("PivotTable54").PivotFields("Date").PivotItems("Oct 21" _
        ).Position = 3


i have a bunch of pivot tables and that require these actions...any way to do this in VBA where it loops through all of them and performs these actions?
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
The following macro will loop through each worksheet within the active workbook, and with each worksheet loops through each pivot table on the worksheet (untested)...

VBA Code:
Option Explicit

Sub test()

    Dim sourceWorkbook As Workbook
    Set sourceWorkbook = ActiveWorkbook
    
    Dim currentWorksheet As Worksheet
    Dim currentPivotTable As PivotTable
    For Each currentWorksheet In sourceWorkbook.Worksheets
        For Each currentPivotTable In currentWorksheet.PivotTables
            With currentPivotTable
                With .PivotFields("Date")
                    .PivotItems("Aug 21").Visible = True
                    'etc
                    '
                    '
                End With
                .PivotSelect "'Sep 21'", xlDataAndLabel + xlFirstRow, True
                With .PivotFields("Date")
                    .PivotItems("Sep 21").Position = 1
                End With
                'etc
                '
                '
            End With
        Next currentPivotTable
    Next currentWorksheet
    
End Sub

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top