共享使用数据模型构build的数据透视表的PivotCache

我只是清理我的工作簿,我已经使用下面的代码来巩固我的PivotCaches(我有大约200清洁之前)。

Sub changeCache() Dim ws As Worksheet Dim pt As PivotTable Dim pc As PivotCache Dim first As Boolean On Error Resume Next For Each ws In ActiveWorkbook.Worksheets ws.Activate For Each pt In ActiveSheet.PivotTables If first = False Then Set pc = pt.PivotCache first = True End If pt.CacheIndex = pc.Index Next pt Next ws End Sub 

这已经将我的PivotCache计数减less到了33。

 Sub CountCaches() MsgBox ActiveWorkbook.PivotCaches.Count End Sub 

它是33而不是1的原因是因为我有数据模型构build的32个数据透视表。

我的问题是:有谁知道如何更改使用数据模型构build的数据透视表,以使用相同的PivotCache?

编辑

我的第二个问题是:多个数据透视表是否都build立在数据模型上

a)引用单个数据模型; 要么

b)每个人都有自己的模型,因此“膨胀”Excel文件

EDIT2

在进一步的研究中,数据模型似乎引用相同数据的数据透视表共享的。 这可以在“连接”中find(在function区的“数据”标签下find)。 理论上,即使代码ActiveWorkbook.PivotCaches.Count统计共享连接的每个数据透视表,并且错误地(?)指示多个高速caching,也不应该使文件“膨胀”。

但是,如果有人能够提供更确切的答案,我将离开赏金。

如果我正确理解你的问题,你只需要设置每个电脑到第一个。 所以,第一遍,给pc一些其他的名字,如pcfirst,然后对于每个剩余的caching,设置pc = pcfirst。 一些来源信息在这里http://www.contextures.com/xlPivot11.html和这里http://www.mrexcel.com/forum/excel-questions/380933-set-multiple-pivot-cache-read-one-cache html的

我还没有真正习惯于数据模型,我不能提供关于这个的无疑的解释。

但是我使用该代码来清理我正在处理的一个报告系统,这可能会帮助您获得更less的PivotCaches:

 Sub Caches_Matches() Dim Ws1 As Worksheet, _ Pt1 As PivotTable, _ Ws2 As Worksheet, _ Pt2 As PivotTable, _ PcNb As Integer PcNb = ActiveWorkbook.PivotCaches.Count MsgBox "PivotCaches.Count = " & PcNb, vbInformation + vbOKOnly, "Before update" On Error Resume Next For Each Ws1 In ActiveWorkbook.Worksheets For Each Pt1 In Ws1.PivotTables 'fix one pt, loop all of them and set same cache if same source For Each Ws2 In ActiveWorkbook.Worksheets For Each Pt2 In Ws2.PivotTables If Pt1.SourceData <> Pt2.SourceData Or Pt1.PivotCache = Pt2.PivotCache Or Pt1.Name = Pt2.Name Then Else Pt2.CacheIndex = Pt1.PivotCache.Index End If Next Pt2 Next Ws2 Next Pt1 Next Ws1 MsgBox "PivotCaches.Count = " & ActiveWorkbook.PivotCaches.Count & Chr(10) & _ "Before update = " & PcNb, vbInformation + vbOKOnly, "After update" End Sub