在宏中运行PivotCache.Refresh时出现应用程序定义或对象定义的错误

时间:2017-01-27 15:20:15

标签: excel-vba pivot-table vba excel

我有8个数据透视表我试图刷新数据库缓存以使用宏(相同的源)。每个数据透视表都命名为PivotTableWPI1,PivotTableWPI2,...,PivotTableWPI8。所以我写了一个For循环来做到这一点。

我收到运行时错误'1004':应用程序定义或对象定义错误

Private Sub resetPivotCache()

Dim wb As Workbook
Set wb = ThisWorkbook
Dim pSheet As Worksheet
Set pSheet = wb.Sheets("Pivot")
Dim i As Integer

    For i = 1 To 8
        pSheet.PivotTables("PivotTableWPI" & i).PivotCache.Refresh
    Next i

End Sub

我实际上也尝试过以下表格:

Private Sub resetPivotCache()

Dim wb As Workbook
Set wb = ThisWorkbook
Dim pSheet As Worksheet
Set pSheet = wb.Sheets("Pivot")
Dim i As Integer

    With pSheet    
        For i = 1 To 8
            PivotTables("PivotTableWPI" & i).PivotCache.Refresh
        Next i
    End With
End Sub

仍然会抛出相同的错误。有任何想法吗?提前谢谢。

0 个答案:

没有答案