Solved

Count rows in a pivot table

Posted on 2001-07-04
9
2,072 Views
Last Modified: 2008-02-01
Hi,
I have a pivot table with a page field in Excel 2000.  I want to get the average of the data, but have this shown outside the pivot table.

I can get the grand total of the data by using the function "getpivotdata", but how do I get the count of the rows?  The number of rows change depending on the page field selected so a named range won't work.

Any ideas?

Thanks.
0
Comment
Question by:davidnz
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 6

Expert Comment

by:xSinbad
ID: 6254117
Here is a good sub that you can modify to your own needs;


Sub DetermineUsedRange(ByRef theRng As Range)
Dim FirstRow As Integer, FirstCol As Integer, _
    LastRow As Integer, LastCol As Integer
On Error GoTo handleError
FirstRow = Cells.Find(What:="*", _
      SearchDirection:=xlNext, _
      SearchOrder:=xlByRows).Row
FirstCol = Cells.Find(What:="*", _
      SearchDirection:=xlNext, _
      SearchOrder:=xlByColumns).Column
LastRow = Cells.Find(What:="*", _
      SearchDirection:=xlPrevious, _
      SearchOrder:=xlByRows).Row
LastCol = Cells.Find(What:="*", _
      SearchDirection:=xlPrevious, _
      SearchOrder:=xlByColumns).Column
Set theRng = Range(Cells(FirstRow, FirstCol), _
    Cells(LastRow, LastCol))
handleError:
End Sub
0
 

Author Comment

by:davidnz
ID: 6254184
Thanks

But I don't want to run a macro each time, is there not a function I can use or add a count item to the pivot table?

Cheers.
0
 
LVL 6

Expert Comment

by:xSinbad
ID: 6254196
Do currently create the pivot table with code?
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:davidnz
ID: 6254206
no i don't.
0
 
LVL 7

Accepted Solution

by:
q2eddie earned 100 total points
ID: 6256059
Hi, davidnz.

#Try This
This assumes that your pivottable's data column starts at B5.

=GETPIVOTDATA(B5,"grand total")/(COUNTA(B5:B10000)-1)

Subtract "1":
one for the data column's grand total.

or...

If you don't know how many items are going to be in the list, then use this code.

=GETPIVOTDATA(B5,"grand total")/(COUNTA(B:B)-3)

Subtract "3":
one for the page field's combobox.
one for the data column's title.
one for the data column's grand total.

Bye. -e2
0
 
LVL 6

Expert Comment

by:bkpchs237
ID: 6256398
davidnz,

There is an option to add average to the pivot table itself.  Enter the pivot table wizard and enter the column that you want to average in the data area.  Don't worry that you already have something that says Sum or Count of the same item already.  This will add another item to your data area in the default format of the program (mine enters "Sum of Junk" for example).  Double-click on this item and change the "Summarize by" option to Average, click on Finish.  This will add an Average value to your table.  

Now when you create or refresh your pivot table data you will have an average item that will adjust to the table automatically.  You can set your table to display only this item, then copy and paste special items as needed thereafter.

Or you could create a new table based on the old one with just the average in it and then paste special the data as needed.

Hope this helps.
0
 

Author Comment

by:davidnz
ID: 6257337
I knew there must be an easy way!  Thanks.
0
 
LVL 7

Expert Comment

by:q2eddie
ID: 6257425
Hi, again.

I'm just curious - I don't mean to be ungrateful...

What made you decide to select my suggestion instead of bkpchs237's?

Bye. -e2
0
 

Author Comment

by:davidnz
ID: 6257450
2 reasons:

I wanted the formula outside the pivot table as stated in my original question.  

I have a number of items to average - so his/her way would mean more work.

Thanks to everyone for responding.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Perhaps more familiar to developers who primarily use VBScript than to developers who tend to work only with Microsoft Office and Visual Basic for Applications (VBA), the Dictionary is a powerful and versatile class, and is useful …
In this article we discuss how to recover the missing Outlook 2011 for Mac data like Emails and Contacts manually.
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question