VBA Pivot Table

sricc

New Member
Joined
Nov 21, 2014
Messages
12
I have created a pivot table with vba that specifies fields such as:
With PT
.PivotFields("Billing").Orientation = xlRowField
.PivotFields("Billing").Position = 1
.PivotFields("Billing").Subtotals(1) = False
.PivotFields("Supplier").Orientation = xlRowField
.PivotFields("Supplier").Position = 2
.PivotFields("Supplier").Subtotals(1) = False
.PivotFields("Year").Orientation = xlColumnField
.PivotFields("Year").Position = 1
.PivotFields("Year").Subtotals(1) = False
.PivotFields("Month").Orientation = xlColumnField
.PivotFields("Month").Position = 2
.PivotFields("Month").Subtotals(1) = False
.PivotFields("Total").Orientation = xlDataField
.PivotFields("Total").Subtotals(1) = False
.RowAxisLayout xlTabularRow
.RepeatAllLabels xlRepeatLabels
End With

For any PivotFields("Year").PivotItems less than the year prior to the current year, I want to hide.

I used the recorder and got:
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Year")
.PivotItems("2017").Visible = False
.PivotItems("2018").Visible = False
.PivotItems("2019").Visible = False
.PivotItems("2020").Visible = False
.PivotItems("2021").Visible = False
End With
...but I don't know how to dynamically execute it with the creation of the PivotTable.
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Assuming that you've added a PivotTable and assigned it to your variable PT, try...

VBA Code:
    Dim pt As PivotTable
    Dim pi As PivotItem
   
    With pt
   
        With .PivotFields("Billing")
            .Orientation = xlRowField
            .Position = 1
            .Subtotals(1) = False
        End With

        With .PivotFields("Supplier")
            .Orientation = xlRowField
            .Position = 2
            .Subtotals(1) = False
        End With

        With .PivotFields("Year")
            .Orientation = xlColumnField
            .Position = 1
            .Subtotals(1) = False
        End With

        With .PivotFields("Month")
            .Orientation = xlColumnField
            .Position = 2
            .Subtotals(1) = False
        End With
       
        .PivotFields("Total").Orientation = xlDataField
       
        .RowAxisLayout xlTabularRow
        .RepeatAllLabels xlRepeatLabels
       
        For Each pi In .PivotFields("Year").PivotItems
            If pi.Value < Year(Date) - 1 Then
                pi.Visible = False
            End If
        Next pi
       
    End With

Hope this helps!
 
Upvote 0
Assuming that you've added a PivotTable and assigned it to your variable PT, try...

VBA Code:
    Dim pt As PivotTable
    Dim pi As PivotItem
  
    With pt
  
        With .PivotFields("Billing")
            .Orientation = xlRowField
            .Position = 1
            .Subtotals(1) = False
        End With

        With .PivotFields("Supplier")
            .Orientation = xlRowField
            .Position = 2
            .Subtotals(1) = False
        End With

        With .PivotFields("Year")
            .Orientation = xlColumnField
            .Position = 1
            .Subtotals(1) = False
        End With

        With .PivotFields("Month")
            .Orientation = xlColumnField
            .Position = 2
            .Subtotals(1) = False
        End With
      
        .PivotFields("Total").Orientation = xlDataField
      
        .RowAxisLayout xlTabularRow
        .RepeatAllLabels xlRepeatLabels
      
        For Each pi In .PivotFields("Year").PivotItems
            If pi.Value < Year(Date) - 1 Then
                pi.Visible = False
            End If
        Next pi
      
    End With

Hope this helps!
With a little formatting, this works great! Thank you so much!
 
Upvote 0

Forum statistics

Threads
1,223,907
Messages
6,175,301
Members
452,633
Latest member
DougMo

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