Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count the visible items in a specific field of PivotTable without looping?

How to count the visible items in a specific field of PivotTable without looping through all the items?

The code:

ActiveSheet.PivotTables(1).PivotFields("MyFieldName").PivotItems.Count

returns the total number of items, no matter if they are selected or not.

I am looking for something like:

ActiveWorkbook.SlicerCaches(1).VisibleSlicerItems.Count 

Update. I want to count how many items are selected in [Country] field of the picture below:

enter image description here

(I burglared the picture from: http://peltiertech.com/referencing-pivot-table-ranges-in-vba/ Hope, Jon won't mind it.)

like image 229
Przemyslaw Remin Avatar asked Nov 26 '25 13:11

Przemyslaw Remin


1 Answers

I know this is an old question, but here is a simple solution

ActiveSheet.PivotTables(1).PivotFields("MyFieldName").VisibleItems.Count

I needed this for a report automation project.

like image 77
mooseman Avatar answered Nov 28 '25 05:11

mooseman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!