PivotTable.TableRange is returning the wrong value!

TheRedCardinal

Active Member
Joined
Jul 11, 2019
Messages
250
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
I am trying to determine the number of rows of data in a Pivot field.

My code looks like this:

Code:
Sub LookupCommon()


'A sub to populate the CSV file with common data


Set WS1 = Sheets("5. Final CSV")
Set WS2 = Sheets("4. PivotTable")


Set MyPivot = WS2.PivotTables(1)
Counter = MyPivot.TableRange1.Rows.Count


End Sub

This returns a value for "Counter" of 76. My table has 73 rows of data. It also has a Grand Total Row, and two rows at the top for the various names = 76.

When I re-run my code with TableRange2.Rows.Count instead, I also get 76.

Surely these 2 numbers cannot be the same? I thought TableRange1 would be data only, and TableRange2 for the whole page?
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
You are misunderstanding what those properties do. They either include or exclude the page filter of a pivot table when telling you where the pivot table sits on a worksheet. If you have no page filter, then they will be identical -- and they will include and subtotal/grandtotal rows. It seems like what you are really looking for might be a count of the items within a pivot field.
Code:
[COLOR=#333333]Counter = MyPivot.pivotfields("aPivotFieldName").pivotItems.count[/COLOR]

This thread might help: https://www.mrexcel.com/forum/excel-questions/943581-vba-count-visible-pivot-items.html
 
Upvote 0
Hi - yes I was misunderstanding and thanks for the explanation. I had interpreted Range1 and being data and Range2 as being all the fluff around the edges, not just the filters.

Thanks for the explanation!
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,217
Members
452,619
Latest member
Shiv1198

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