有什么办法来检查一个数据透视caching是否需要刷新?

编辑:

谢谢你的回答。 然而,这不适合我的目的。 我创build了一些虚拟数据和数据透视表,然后改变了一些底层数据,你的代码仍然表示caching与数据是一致的。 我想这是因为你正在寻找整体范围的变化,而不是范围内的变化。

我一直在玩弄和使用文档,尤其是您在下面使用的PivotCache.SourceData成员,我不认为有可能做我想做的事情。 我发现最好的是从原始源数据创build一个新的数据透视caching,然后以某种方式比较它们(虽然这是不可能的使用直接相等:

Sub checkCache() Dim ptTbl As PivotTable, pvtCache As PivotCache Set ptTbl = Sheets("Sheet2").PivotTables("PivotTable2") Set pvtCache = ActiveWorkbook.PivotCaches.Create(xlDatabase, ptTbl.PivotCache.SourceData) If pvtCache = ptTbl.PivotCache Then 'error on this line, cannot compare two pivot caches like this MsgBox "True" Else MsgBox "False" End If End Sub 

如果还有更好的想法,我会把这个问题搁置几天。

简洁版本

当数据透视表的底层数据发生变化,他们需要刷新数据透视caching时,我需要向用户强调。 由于性能问题,我不能只刷新每次交互的数据透视表。

目前,我正在用IF公式检​​查数据透视表总数是否仍等于工作表总数,但这对于非数字数据不起作用。 有没有更好的办法?

长版

我有几千行数据被分类如下(即可能会改变的几个类别和数字)。

 Name Category 1 Category 2 Number Number 2 Category 3... etc. Angela AX 123 Bob AY 442 Bob AY 566 Charlie BX 1445 Angela AX 5641231 Dave BY 435 Dave BY 45645 Charlie BX 567 

我的问题是,当数据透视表需要刷新时(即源数据与数据透视caching不同时),我想向团队(以及我自己)强调。

工作簿很大,需要很长时间来计算,所以我不希望每次用户与工作表交互时都自动刷新数据透视表。 这是一个标准的公司工作手册,尽pipe我已经做了一些改进来加快速度,但是我不能做出大规模的改变。

我知道我可以使用SUMIF等来自动更新汇总表,但在这种情况下我不能这样做。

目前,我只是将IF语句与一些明亮的红色条件格式相结合

 =IF(total from summing up workbook directly = pivot table total, "Pivot table OK", "Pivot table needs refreshing") 

所以用户会看到一个明亮的红色单元,告诉他们刷新数据透视表。

我的问题是,这只适用于数字数据。 如果对非数字数据进行了更改,数据透视表将需要刷新,但是这种方法不起作用。

我可以设置很多不同的表格来计算每个类别中每个标签的出现次数,然后根据数据透视表caching中的某些数据透视表检查这个表格,但是这将是很多工作。 这可以在Excel或VBA中以其他方式完成吗?

您可以将当前PivotTable.PivotCache.SourceData与工作表中的数据(占用数据透视表)进行比较。

如果String值相同,则不需要更新PivotCache ,之后再刷新PivotTable 。 如果值不相同,则表示数据结构已更改(更多行和列已被添加/删除),您需要更新PivotCacheRefresh PivotTable

 Option Explicit Sub CheckPivotCacheRefresh() Dim PvtSht As Worksheet Dim DataSht As Worksheet Dim PTTbl As PivotTable Dim SrcData As String Dim LastRow As Long Dim LastCol As Long Set DataSht = Worksheets("Sheet1") ' modify "Sheet1" to your sheet's name where your Pivot Data exists Set PvtSht = Worksheets("Sheet2") ' modify "Sheet2" to your sheet's name where your Pivot Table exists ' find occupied Data Range for Pivot (starting from Cell "A1") With DataSht LastRow = .Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row LastCol = .Cells.Find("*", searchorder:=xlByColumns, searchdirection:=xlPrevious).Column SrcData = .Name & "!" & .Range(.Cells(1, 1), .Cells(LastRow, LastCol)).Address(ReferenceStyle:=xlR1C1) End With ' add this line in case the Pivot table doesn't exit On Error Resume Next Set PTTbl = PvtSht.PivotTables("PivotTable1") ' set "PivotTable1" in your Pivot sheet With PTTbl If .PivotCache.SourceData Like SrcData Then MsgBox "Pivot Cache Source Data and current Data are identical : " & SrcData Else MsgBox "Pivot Cache Source Data is : " & .PivotCache.SourceData & ", " & vbCr & _ " while current Data is : " & SrcData End If End With End Sub