Solved

count of pivot tables in a workbook

Posted on 2010-09-06
9
882 Views
Last Modified: 2013-11-26
How to determine via vba the count of pivot tables (How many tables exist in a workbook ) in a workbook so that once can store there names in array(dynamic) and perform a series of operations
0
Comment
Question by:Rajnish_Sharma_ibm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 50
ID: 33609500
Hello Rajnish,

the code below will count the pivot tables in all sheets of the active workbook.

cheers, teylyn
Sub CountPivotTables()
Dim ws As Worksheet
Dim i As Integer
i = 0
For Each ws In ActiveWorkbook.Worksheets
    For Each PivotTable In ws.PivotTables
        i = i + 1
    Next PivotTable
Next ws
Debug.Print i

End Sub

Open in new window

0
 
LVL 17

Expert Comment

by:calacuccia
ID: 33609503
Dim i as Long
Dim  wb as workbook
Dim ws as worksheet

i = 0
Set wb as Workbookds("MyWorkbook.xls")
For each ws in wb.Worksheets
   i = i + ws.PivotTables.Count
Next ws
MsgBox "There are " & i & " Pivot Tables in your workbook"
0
 
LVL 50
ID: 33609517
cal, just learned something. Thanks, teylyn
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Expert Comment

by:calacuccia
ID: 33609524
PivotTables.Count?
0
 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 125 total points
ID: 33609528
The following will return both the sheet names and pivot table names so you can address them appropriately as:

activeworkbook.sheets(arrPivots(arrIndex, 0))(.pivottables(arrPivots(arrIndex, 0))

Where arrindex is the index number within the array for the tables ... possibly via a loop.

Chris
Sub pivArr()
Dim arrPivots() As String
Dim arrIndex As Integer
Dim piv As Variant
Dim cnt As Integer
Dim ws As Worksheet

    For Each ws In ThisWorkbook.Worksheets
        cnt = cnt + ws.PivotTables.Count
    Next
    arrIndex = -1
    For Each ws In ThisWorkbook.Worksheets
        For Each piv In ws.PivotTables
            arrIndex = arrIndex + 1
            ReDim Preserve arrPivots(0 To cnt - 1, 1)
            arrPivots(arrIndex, 0) = ws.Name
            arrPivots(arrIndex, 1) = piv.Name
        Next
    Next
End Sub

Open in new window

0
 
LVL 50
ID: 33609663
@cal. precisely. I'm baby-stepping through VBA, as it is, spreading my "wisdom" where I can. LOL. Seeing the latest comment, I guess Chris has it nailed. Again. Cheers, Chris.
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 33609691
>>> I'm baby-stepping through VBA, as it is, spreading my "wisdom" where I can

And trouncing me at almost every opportunity ... with your level of detail :o)

Chris
0
 

Author Closing Comment

by:Rajnish_Sharma_ibm
ID: 33609836
Thanks for speedy and accurate solution!
0
 
LVL 50
ID: 33610367
>> with your level of detail

You can take the girl out of Germany, but you can't take Germany out of the girl! :-))

enjoy your points!
0

Featured Post

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.

Question has a verified solution.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

719 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