在另一篇文章中,我获得了以下VBA代码,以附加到"刷新"用于刷新数据透视数据的按钮:
Option Explicit
Sub Button5_Click()
Dim PvtTbl As PivotTable
Dim PvtCache As PivotCache
Dim PvtDataRng As Range
' Set/Update Pivot Data Range
Set PvtDataRng = Worksheets("PivotDataSheet").Range("A1:E100") ' <-- just an example
' Create/Update Pivot Cache
Set PvtCache = ActiveWorkbook.PivotCaches.Add(xlDatabase, PvtDataRng)
' Set the Pivot Table (already created, otherwise need to create it)
Set PvtTbl = Worksheets("DD").PivotTables("test")
' refresh the Pivot Table with the latest Pivot Cache
With PvtTbl
.ChangePivotCache PvtCache
.RefreshTable
End With
End Sub
如何修改此代码以同时刷新同一工作表中的第二个数据透视图?让我们称之为第二个支点&#34; test2&#34;它也在DD工作表中。
谢谢, Kevbo
答案 0 :(得分:1)
如果您在“DD”工作表中只有这两个 MERGE tabelName WITH (HOLDLOCK) AS target
USING (SELECT Column1, Column2,Column3,Column4) AS source (Column1, Column2,Column3,Column4) // here goes your source
ON (target.Column1 = source.Column1)
WHEN MATCHED THEN
UPDATE SET Column2 = source.Column2, Column3 =source.Column3
WHEN NOT MATCHED THEN
INSERT ( Column1, Column2,Column3,Column4)
VALUES ( source.Column1, source.Column2,source.Column3,source.Column4) ;
,并且您希望根据相同的PivotTable
更新这两个,请替换以下PivotCache
循环:< / p>
For
使用以下With PvtTbl
.ChangePivotCache PvtCache
.RefreshTable
End With
循环:
For
编辑1 :' refresh all Pivot Tables in "DD" worksheet
For Each PvtTbl In Worksheets("DD").PivotTables
With PvtTbl
.ChangePivotCache PvtCache
.RefreshTable
End With
Next PvtTbl
Set
循环中的PivotCache
,并在结尾清除它。使用下面的循环,并从之前的位置删除For
行。
Set PvtCache
答案 1 :(得分:0)
我自己没有对此进行测试,但您可以尝试告诉我它是否有效。看起来它可能适合你
Function refreshPTcontent()
Dim ws As Worksheet
Dim pt As PivotTable
Dim pf As PivotField
Dim pi As PivotItem
On Error Resume Next
For Each ws In ActiveWorkbook.Worksheets
For Each pt In ws.PivotTables
pt.RefreshTable
Next pt
Next ws
End Function