Solved

count of pivot tables in a workbook

Posted on 2010-09-06
9
829 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
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 50

Expert Comment

by:Ingeborg Hawighorst
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

Expert Comment

by:Ingeborg Hawighorst
ID: 33609517
cal, just learned something. Thanks, teylyn
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

 
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

Expert Comment

by:Ingeborg Hawighorst
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

Expert Comment

by:Ingeborg Hawighorst
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
randomize data from excell cell 15 47
Turn date into age 17 33
Adding Additional Criteria to a formula 17 47
Excel Split Employee Name into Lname Fname Mname 3 17
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

831 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