简体   繁体   English

Excel VBA重新应用数据透视表数据源

[英]Excel VBA re-apply pivot table data source

I have a macro that copies two sheets from my workbook to their own workbook. 我有一个宏,可以将工作表中的两张纸复制到自己的工作簿中。 One sheet has some data defined with a named range, the second has multiple pivot tables, all with the data source as said named range candData . 一张纸上有一些用命名范围定义的数据,第二张纸上有多个数据透视表,所有数据源都以所述命名范围candData

Once the sheets have copied my slicers lose some of their Report Connections. 一旦复制了工作表,我的切片器就会失去一些“报告连接”。

在此处输入图片说明

In order to reconnect them, I have to manually select each pivot table and set the data source again, even though the data source is already set. 为了重新连接它们,即使已设置数据源,我也必须手动选择每个数据透视表并再次设置数据源。 (I simply click Change Data Source and immediately click OK, without actually changing anything.) (我只需单击“ Change Data Source然后立即单击“确定”,而无需实际更改任何内容。)

Once I have done this for each pivot table all report connections are showing again. 对每个数据透视表完成此操作后,所有报表连接都将再次显示。

在此处输入图片说明

I have the following macro to loop through each pivot table and re apply the data source, then reconnect each slicer connection, however even after the data source has been applied, I still have to manually select each pivot table in order to reconnect the slicers. 我具有以下宏来循环遍历每个数据透视表并重新应用数据源,然后重新连接每个切片器连接,但是即使在应用了数据源之后,我仍然必须手动选择每个数据透视表以重新连接切片器。

I am getting no errors, it steps through each pivot table as expected and resets the data source. 我没有收到任何错误,它按预期方式遍历了每个数据透视表并重置了数据源。 Any clues why the report connections only work when I set the data source manually instead of through the following? 为什么只有当我手动设置数据源而不是通过以下方式设置报表源时,报表连接才起作用?

Sub setSlicerSource()

Dim MyPivot As PivotTable
Dim slCaches As SlicerCaches
Dim slCache As SlicerCache

Set slCaches = ActiveWorkbook.SlicerCaches

With ActiveWorkbook
    For Each MyPivot In .Sheets("Pivots").PivotTables
        MyPivot.ChangePivotCache .PivotCaches.Create(SourceType:=xlDatabase, SourceData:="candData")
    Next MyPivot

    For Each slCache In slCaches
        For Each MyPivot In .Sheets("Pivots").PivotTables
            slCache.PivotTables.AddPivotTable MyPivot
        Next MyPivot
    Next slCache
End With
End Sub

I'm not sure if this will actually solve your problem, but it's the first thing I'd try fixing regardless: 我不确定这是否能真正解决您的问题,但这是我尝试修复的第一件事,无论如何:

This line currently creates a new PivotCache for each PivotTable: 此行当前为每个数据透视表创建一个新的PivotCache:

MyPivot.ChangePivotCache .PivotCaches.Create(SourceType:=xlDatabase, SourceData:="candData")

Instead, create a PivotCache for PivotTables(1) first before the loop: 相反,请在循环之前首先为PivotTables(1)创建一个PivotCache:

.Sheets("Pivots").PivotTables(1).ChangePivotCache .PivotCaches.Create(SourceType:=xlDatabase, SourceData:="candData")

Then in the loop, set all PivotTables to use the cache of PivotTables(1) 然后在循环中,将所有数据透视表设置为使用数据透视表的缓存(1)

MyPivot.ChangePivotCache .Sheets("Pivots").PivotTables(1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM