Counting values in a pivot table through VBA

rdw72777

Well-known Member
Joined
Apr 5, 2005
Messages
723
So I have a pivot table and I'd like it to always re-populate with the rows always collapsed when data is refreshed or the data range changed.

Basically I have row labels for Region, State, DMA in that sequence in my pivot table. If a new region shows up, all of the States/DMA/s in the pivot table show up and are not collapsed, same with new states.

What I'd like to do is figure out a way I can go through all of the values in the Region pivot item and set the "ShowDetail" equal to false.

I can hardcode it using code similar to that below but I'm wary of that as it won't address new regions/states/DMA's.

Code:
ActiveSheet.PivotTables("PivotTable1").PivotFields("Region").PivotItems( _
        "East").ShowDetail = False

I'm sure there must be a way to count the number of regions in the "region" pivot item and loop through to set them all to ShowDetail=False, but I can't figure out how to do a count on the pivotitem or pivotfield.
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Hi Ryan,

In xl2007 you can collapse the field without iterating through each item.
Code:
Sub CollapseField1()
    ActiveSheet.PivotTables("PivotTable1") _
        .PivotFields("Region").ShowDetail = False
End Sub

Here are two ways to iterate just in case the code above doesn't work for you.

Code:
Sub CollapseField2()
    Dim pti As PivotItem
    With ActiveSheet.PivotTables("PivotTable1")
        For Each pti In .PivotFields("Region").PivotItems
            pti.ShowDetail = False
        Next
    End With
End Sub
 
Sub CollapseField3()
    Dim lngIdx As Long
    With ActiveSheet.PivotTables("PivotTable1").PivotFields("Region")
        For lngIdx = 1 To .PivotItems.Count
            .PivotItems(lngIdx).ShowDetail = False
        Next
    End With
End Sub
 
Upvote 0
Jerry

thanks, the code you gave worked perfect. It made me sad to know that changing from "pivotItem" to "PivotField" could have saved me hours of work the past 6 months, but I'm happy to never have to do it again.

Thanks again,

Ryan
 
Upvote 0

Forum statistics

Threads
1,224,542
Messages
6,179,424
Members
452,914
Latest member
echoix

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