Tip: Other languages are Google-Translated. You can visit the English version of this link.
Log in
x
or
x
x
Register
x

or

How to list all pivot tables from a workbook?

Supposing you have a large workbook which contains multiple pivot tables, now, you want to list all the pivot tables in this workbook, is it possible? Of course, the following VBA code in this article will do you a favor. To know more details, please read the below article.

List all pivot tables from a workbook with VBA code

Office Tab Enable Tabbed Editing and Browsing in Office, and Make Your Work Much Easier...
Kutools for Excel Solves Most of Your Problems, and Increases Your Productivity by 80%
  • Reuse Anything: Add the most used or complex formulas, charts and anything else to your favorites, and quickly reuse them in the future.
  • More than 20 text features: Extract Number from Text String; Extract or Remove Part of Texts; Convert Numbers and Currencies to English Words.
  • Merge Tools: Multiple Workbooks and Sheets into One; Merge Multiple Cells/Rows/Columns Without Losing Data; Merge Duplicate Rows and Sum.
  • Split Tools: Split Data into Multiple Sheets Based on Value; One Workbook to Multiple Excel, PDF or CSV Files; One Column to Multiple Columns.
  • Paste Skipping Hidden/Filtered Rows; Count And Sum by Background Color; Send Personalized Emails to Multiple Recipients in Bulk.
  • Super Filter: Create advanced filter schemes and apply to any sheets; Sort by week, day, frequency and more; Filter by bold, formulas, comment...
  • More than 300 powerful features; Works with Office 2007-2019 and 365; Supports all languages; Easy deploying in your enterprise or organization.

arrow blue right bubble List all pivot tables from a workbook with VBA code


The following VBA code can help you to list all the pivot table names along with their attributes, such as source data range, worksheet name, refreshed date and so on.

1. Open your workbook that you want to list all the pivot tables.

2. Hold down the ALT + F11 keys, and it opens the Microsoft Visual Basic for Applications window.

3. Click Insert > Module, and paste the following code in the Module Window.

VBA code: List all pivot tables from a workbook

Sub ListPivotsInfor()
'Update 20141112
    Dim St As Worksheet
    Dim NewSt As Worksheet
    Dim pt As PivotTable
    Dim I, K As Long
    Application.ScreenUpdating = False
    Set NewSt = Worksheets.Add
    I = 1: K = 2
    With NewSt
        .Cells(I, 1) = "Name"
        .Cells(I, 2) = "Source"
        .Cells(I, 3) = "Refreshed by"
        .Cells(I, 4) = "Refreshed"
        .Cells(I, 5) = "Sheet"
        .Cells(I, 6) = "Location"
        For Each St In ActiveWorkbook.Worksheets
            For Each pt In St.PivotTables
                I = I + 1
                .Cells(I, 1).Value = pt.Name
                .Cells(I, 2).Value = pt.SourceData
                .Cells(I, 3).Value = pt.RefreshName
                .Cells(I, 4).Value = pt.RefreshDate
                .Cells(I, 5).Value = St.Name
                .Cells(I, 6).Value = pt.TableRange1.Address
            Next
        Next
        .Activate
    End With
    Application.ScreenUpdating = True
End Sub

4. Then press F5 key to run this code, all the pivot table names, source data range, worksheet name and other attributes are listed in a new worksheet which placed in the front of your active worksheet as following screenshot shown:

doc-list-all-pivottable-1


Related articles:

How to check if a pivot table exists in a workbook?

How to add multiple fields into pivot table?


Kutools for Excel Solves Most of Your Problems, and Increases Your Productivity by 80%

  • Reuse: Quickly insert complex formulas, charts and anything that you have used before; Encrypt Cells with password; Create Mailing List and send emails...
  • Super Formula Bar (easily edit multiple lines of text and formula); Reading Layout (easily read and edit large numbers of cells); Paste to Filtered Range...
  • Merge Cells/Rows/Columns without losing Data; Split Cells Content; Combine Duplicate Rows/Columns... Prevent Duplicate Cells; Compare Ranges...
  • Select Duplicate or Unique Rows; Select Blank Rows (all cells are empty); Super Find and Fuzzy Find in Many Workbooks; Random Select...
  • Exact Copy Multiple Cells without changing formula reference; Auto Create References to Multiple Sheets; Insert Bullets, Check Boxes and more...
  • Extract Text, Add Text, Remove by Position, Remove Space; Create and Print Paging Subtotals; Convert Between Cells Content and Comments...
  • Super Filter (save and apply filter schemes to other sheets); Advanced Sort by month/week/day, frequency and more; Special Filter by bold, italic...
  • Combine Workbooks and WorkSheets; Merge Tables based on key columns; Split Data into Multiple Sheets; Batch Convert xls, xlsx and PDF...
  • More than 300 powerful features. Supports Office/Excel 2007-2019 and 365. Supports all languages. Easy deploying in your enterprise or organization. Full features 30-day free trial.
kte tab 201905

Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier

  • Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
  • Open and create multiple documents in new tabs of the same window, rather than in new windows.
  • Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!
officetab bottom
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Fuzz-Head · 3 months ago
    Thanks for the post, I cleaned up the code a little by defining I and Long and deleting the ref to K since it wasn't used. Lastly I added a line just below your .Activate to show .Columns.AutoFit
  • To post as a guest, your comment is unpublished.
    Abraham Garzon · 7 months ago
    This is awesome! I have been looking for a code like this one, but with the addition of showing all active fields per table. I need to clean up the source tables from unnecessary fields (too heavy) and they feed a book of over 300 pivots. I'd prefer not to go one pivot table at a time to figure out which fields I can eliminate... If you could show me how, it would be incredible... Thanks!
  • To post as a guest, your comment is unpublished.
    Vinicius · 8 months ago
    Amazing!!! thksss!!!!
  • To post as a guest, your comment is unpublished.
    Mark · 1 years ago
    Saved me from a major headache!
    Couldn't find what pivot table was causing a "Refresh All Error"
    Boom Listed with locations, Thank you so much
  • To post as a guest, your comment is unpublished.
    chris · 1 years ago
    For Connection Only info the Data connection info isa property of the PivotCache
    I have a workbook that has over 40 pivot tables with a mix of Excel Tables and SQL server Data Connections. I use the following code to keep track of them


    Sub GetPivotTableInfo()

    Dim wb As Workbook
    Dim pvt As PivotTable
    Dim wsheet As Worksheet
    Dim pc As PivotCache

    Set wb = ActiveWorkbook
    For Each wsheet In wb.Worksheets
    For Each pvt In wsheet.PivotTables
    Debug.Print wsheet.Name & ": " & pvt.Name
    Set pc = wb.PivotCaches(pvt.CacheIndex)
    If pc.SourceType = xlDatabase Then
    Debug.Print pc.SourceData
    Else
    If pc.QueryType = xlOLEDBQuery Then
    Debug.Print pc.Connection
    Debug.Print pc.SourceConnectionFile
    Debug.Print pc.WorkbookConnection.Name
    Debug.Print pc.CommandText
    End If
    End If


    Next pvt
    Next wsheet
  • To post as a guest, your comment is unpublished.
    Turnabout · 1 years ago
    Very powerful. Thanks so much.
  • To post as a guest, your comment is unpublished.
    HDF · 2 years ago
    Thanks for this. It's a great piece of code, very useful.

    As others have posted, I think it doesn't work for pivot tables whose underlying data source is based on a Connection Only data query.

    I have a workbook with one data table. I subsequently create more refined workbook data queries based on this one table's data. The queries are connection only (avoiding an unnecessary increase in the size of the workbook). I then created pivot tables that rely on the data in the workbook queries.

    When I run the code, it works fine for traditionally sourced pivot tables, but it hits a run-time error when it gets to the pivot tables based on data in Workbook Queries. Specifically; it gives a Run-time error "1004": Application-defined or object defined error. The line of code where the error occurs is:

    .cells(I, 2).value = pt.SourceData

    Thanks for providing the code and I hope the above helps you refine it.
  • To post as a guest, your comment is unpublished.
    Ross · 2 years ago
    I have tried to run this in excel 2013 and I get the Run-time error '1004': Application-defined or object-defined error.

    If comment out the .Cells(I, 2).Value = pt.SourceData line it will run fine, can you advise me what I might need to do to get the SourceData part to work?

    Thanks Ross
    • To post as a guest, your comment is unpublished.
      EdH · 2 years ago
      [quote name="Ross"]I have tried to run this in excel 2013 and I get the Run-time error '1004': Application-defined or object-defined error.

      If comment out the .Cells(I, 2).Value = pt.SourceData line it will run fine, can you advise me what I might need to do to get the SourceData part to work?

      Thanks Ross[/quote]

      I got the same error. I think it is bombing when the source for a pivot table is the Excel Data Model and used by PowerPivot.
  • To post as a guest, your comment is unpublished.
    Dave · 3 years ago
    I needed to fix a file that had over 60 pivot tables in it. Initially clicked each one at a time to fix ( as is the case for times when someone 'unlinks' them but I could not fina all the wrong ones. It found the last one on a few columns that were hidden on the original) . Nice piece of code !
  • To post as a guest, your comment is unpublished.
    Pat Z · 3 years ago
    This was a massive find for me! Wish I had looked for this solution three days ago. It would have saved me hours!! Thanks!
  • To post as a guest, your comment is unpublished.
    Doaa · 3 years ago
    excellent. thank you.
  • To post as a guest, your comment is unpublished.
    Drip LeBuk · 4 years ago
    Good stuff. Could include this snippet to make the pivot table name a hyperlink.

    .Cells(I, 1).Parent.Hyperlinks.Add Anchor:=.Cells(I, 1) _
    , Address:="" _
    , SubAddress:="'" + St.Name + "'!" + Split(pt.TableRange1.Address, ":")(0) _
    , TextToDisplay:=pt.Name
    With .Cells(I, 7).Font
    .ColorIndex = xlAutomatic
    .Underline = xlUnderlineStyleNone
    End With
    With .Cells(I, 7).Characters(Start:=1, Length:=Len(pt.Name)).Font
    .Underline = xlUnderlineStyleSingle
    .Color = -4165632
    End With