Pivot table row and column count

Smithy88

New Member
Joined
Jan 5, 2012
Messages
12
Hi,

I am trying to write a macro to get the number of rows and columns in a pivot table and then transfer that macro into Delphi. I can interact with pivottables using say

NoColsSFInPivot1 := Excel.ActiveSheet.PivotTablesPivotTableName).datafields.count;

which counts the datafields in the PT, but how can I count the number of rows and the number of columns?

Thanks

Paul
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Paul I have created a Pivot table and placed it on a sheet. I have then used the following code to select the rows and then add a message box to count the rows and repeated it for counting the columns. I hope this helps.

<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> countPivot()<br><SPAN style="color:#00007F">Dim</SPAN> pt <SPAN style="color:#00007F">As</SPAN> PivotTable<br><SPAN style="color:#00007F">Set</SPAN> pt = ActiveSheet.PivotTables(1)<br>pt.RowRange.Select<br>MsgBox pt.RowRange.count<br>pt.ColumnRange.Select<br>MsgBox pt.ColumnRange.count<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>
 
Upvote 0
Hi,

Thanks, yes that works great, used it without the select. Well actually the rows seems to produce the correct count but the column count is coming through at +1 of the actual count for some reason. I have found

NoRowsSFInPivot1 := Excel.ActiveSheet.PivotTables(PivotTableName).TableRange2.Rows.Count;
NoColsSFInPivot1 := Excel.ActiveSheet.PivotTables(PivotTableName).TableRange2.Columns.Count;

but this gives the number of rows of the table entirely which is not always the number of rows. Column count always works though.

Thanks

Paul
 
Upvote 0
Paul,

I hope it helps you find a solution. I suppose you can always get the count and take one off, obviously this also depends on the design of your Pivot.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,287
Members
452,631
Latest member
a_potato

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