公式引用整个数据透视表?

时间:2009-10-21 22:52:53

标签: excel excel-vba pivot-table vba

我有一堆带有详细数据集和数据透视表的工作表。在摘要表中,我想只显示数据透视表。 (当然,我宁愿保持DRY而不是创建一个全新的集合。)我如何引用旧的数据透视表?

如有必要,我可以使用VBA执行此操作。

1 个答案:

答案 0 :(得分:1)

此子目录将使数据透视表保持活动状态。如果你不想要它,你可以将PasteValues覆盖在它们上面。

Sub SummarizePivotTables()
    Dim wb As Workbook, ws As Worksheet, ss As Worksheet, pt As PivotTable
    Dim pasteRow As Long
    Const rowsBetween As Long = 1

    Set wb = ThisWorkbook
    Set ss = wb.Worksheets("Summary")
    pasteRow = 1 'first table row'

    For Each ws In wb.Worksheets
        For Each pt In ws.PivotTables
            'change this to TableRange1 if you do not want the page field included'
            With pt.TableRange2
                .Copy ss.Range("A" & pasteRow)
                pasteRow = pasteRow + .Rows.Count + rowsBetween
            End With
        Next pt
    Next ws
End Sub