如何更新几个数据透视表的范围?

时间:2018-08-08 06:15:20

标签: excel excel-vba

我想更新几个数据透视表的范围,因为它将向其数据源提供新的输入。因此,用作这些数据透视表的源的范围将更大。

但是我不知道为什么以下代码不更新范围:

Sub TEST()
Dim ws As Worksheet
Dim wsData As Worksheet
Dim wsPT As Worksheet
Dim lastCol As Long, LastRow As Long
Dim rngPT As Range
Dim PCache As PivotCache
Dim PT As PivotTable

    Set wsData = Worksheets("Combined Table")
    Set wsPT = Worksheets("Calculation")

With wsData
    LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row - 1
    Set rngPT = .Cells(2, 1).Resize(LastRow, 61)
End With

    Set PCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=rngPT.Address)

For Each PT In wsPT.PivotTables
    PT.PivotCache.Refresh
Next PT

数据透视表是通过以下方式创建的:

Dim wsData As Worksheet
Dim wsPT As Worksheet
Dim lastCol As Long, LastRow As Long
Dim rngPT As Range
Dim PCache, PCache1 As PivotCache
Dim PT, PT2, PT3, PT4, pT5, PT6, PT7 As PivotTable
 Set wsData = Worksheets("Combined Table")
    Set wsPT = Worksheets("Calculation")
    With wsData
        LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row - 1
        Set rngPT = .Cells(2, 1).Resize(LastRow, 61)
    End With
    wsData.Activate
        Set PCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=rngPT.Address)
        Set PT = PCache.CreatePivotTable(TableDestination:="Calculation!R1C1", TableName:="TCD_1")
        Set PT2 = PCache.CreatePivotTable(TableDestination:="Calculation!R20C1", TableName:="TCD_2")
        Set PT3 = PCache.CreatePivotTable(TableDestination:="Calculation!R40C1", TableName:="TCD_3")
        Set PT4 = PCache.CreatePivotTable(TableDestination:="Calculation!R60C1", TableName:="TCD_4")
        Set pT5 = PCache.CreatePivotTable(TableDestination:="Calculation!R80C1", TableName:="TCD_5")
        Set PT6 = PCache.CreatePivotTable(TableDestination:="Calculation!R100C1", TableName:="TCD_6")
        Set PT7 = PCache.CreatePivotTable(TableDestination:="Calculation!R120C1", TableName:="TCD_7")

如何更新数据透视表的范围?

0 个答案:

没有答案