提示:其他語言是Google翻譯的。 你可以訪問 English 版本。
登入
x
or
x
x
註冊
x

or

如何列出工作簿中的所有數據透視表?

假設您有一個包含多個數據透視表的大型工作簿,現在想要列出此工作簿中的所有數據透視表,是否有可能? 當然,這篇文章中的下面的VBA代碼會幫你一個忙。 要了解更多的細節,請閱讀下面的文章。

使用VBA代碼列出工作簿中的所有數據透視表

Office選項卡在Office中啟用選項卡式編輯和瀏覽,使您的工作更輕鬆......
Kutools for Excel解決了您的大多數問題,並使您的生產率提高了80%
  • 重用任何東西: 將最常用或最複雜的公式,圖表和其他任何內容添加到您的收藏夾中,並在將來快速重複使用它們。
  • 超過20文本功能: 從文本字符串中提取數字; 提取或刪除部分文本; 將數字和貨幣轉換為英語單詞...
  • 合併工具:多個工作簿和表格合二為一; 合併多個單元格/行/列而不丟失數據; 合併重複行和總和...
  • 拆分工具:根據價值將數據拆分為多個表格; 一個工作簿到多個Excel,PDF或CSV文件; 一列到多列......
  • 粘貼跳過 隱藏/過濾行; 數和總和 按背景顏色; 創建郵件列表和 通過Cell的價值發送電子郵件...
  • 超級過濾器: 創建高級過濾方案並應用於任何工作表; 分類 按週,日,頻率等; 過濾 通過大膽,公式,評論......
  • 超過300強大的功能; 與Office 2007-2019和365一起使用; 支持所有語言; 在您的企業或組織中輕鬆部署。

箭頭藍色右泡 使用VBA代碼列出工作簿中的所有數據透視表


以下VBA代碼可以幫助您列出所有數據透視表名稱及其屬性,例如源數據范圍,工作表名稱,刷新日期等。

1. 打開您想要列出所有透視表的工作簿。

2。 按住 ALT + F11 鍵,然後打開 Microsoft Visual Basic for Applications窗口.

3。 點擊 插入 > 模塊,並將以下代碼粘貼到 模塊窗口.

VBA代碼:列出工作簿中的所有數據透視表

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。 然後按 F5 運行此代碼的關鍵,所有數據透視表名稱,源數據范圍,工作表名稱和其他屬性都列在放置在活動工作表前面的新工作表中,如下面的屏幕截圖所示:

文檔列表,所有數據透視表,1


相關文章:

如何檢查數據透視表是否存在於工作簿中?

如何將多個字段添加到數據透視表中?


Kutools for Excel解決了您的大多數問題,並使您的生產率提高了80%

  • 重用: 快速插入 複雜的公式,圖表 以及你以前用過的任何東西; 加密單元格 密碼; 創建郵件列表 並發送電子郵件...
  • 超級方程式酒吧 (輕鬆編輯多行文字和公式); 閱讀佈局 (輕鬆讀取和編輯大量單元格); 粘貼到過濾範圍...
  • 合併單元格/行/列 不丟失數據; 分裂細胞含量; 組合重複的行/列...防止重複的細胞; 比較範圍...
  • 選擇複製或唯一 行; 選擇空行 (所有細胞都是空的); 超級查找和模糊查找 在許多工作簿中; 隨機選擇......
  • 精確複製 多個單元格而不更改公式參考; 自動創建參考 多張表; 插入項目符號,複選框等等......
  • 提取文本,添加文本,按位置刪除, 刪除空間; 創建和打印分頁小計; 在單元格內容和註釋之間轉換...
  • 超級過濾器 (將過濾方案保存並應用到其他工作表); 高級排序 按月/週/日,頻率等; 特殊過濾器 用粗體,斜體......
  • 結合工作簿和工作表; 根據鍵列合併表; 將數據拆分為多個表格; 批量轉換xls,xlsx和PDF...
  • 超過300強大的功能。 支持Office / Excel 2007-2019和365。 支持所有語言。 在您的企業或組織中輕鬆部署。 全功能30天免費試用。
kte tab 201905

Office選項卡為Office提供選項卡式界面,使您的工作更輕鬆

  • 在Word,Excel,PowerPoint中啟用選項卡式編輯和閱讀,Publisher,Access,Visio和Project。
  • 在同一窗口的新選項卡中打開並創建多個文檔,而不是在新窗口中。
  • 通過50%提高您的工作效率,每天為您減少數百次鼠標點擊!
官方底部
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 · 1 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 · 5 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 · 5 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