Solved

Count rows in a pivot table

Posted on 2001-07-04
9
1,956 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Some time ago I was asked to create a VBA function that would calculate a check digit for an input number, using the following procedure: First, sum up all the individual digits in the number If that sum value has more than one digit, then sum up …
In case Office 2010 has not been deployed in your environment, this article may be quite useful. In our office, we wanted a way to deploy Microsoft Office Professional Plus 2010 through an automated batch file via logon script. This article is docum…
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…

816 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now