切片器连接不显示数据透视表更改后数据源

时间:2019-07-08 17:41:09

标签: c# excel vba vb.net interop

我在两个不同的工作表(Sheet1和Sheet2)中有两个数据透视表,它们从同一个工作表(Sheet3)中获取数据。在Sheet1上的数据透视表上创建切片器。如果我们要报告连接,则可以在列表中看到两个数据透视表。

现在,我正在一张一张地动态更改两个数据透视表的数据透视源。唯一的变化是范围被扩展为包括由不同过程复制的新行。执行代码后,报表连接不再显示两个数据透视表。它只显示一个。

使用以下代码更改数据透视表数据源。


                    Dim objwrksheet As Worksheet = mWorkBook.Worksheets(mPivotWorksheetname)
                    Dim objwrksheet2 As Worksheet = mWorkBook.Worksheets(mDataWorksheetname)
                    If Not IsNothing(objwrksheet) Then
                        Dim objpivottable As PivotTable = objwrksheet.PivotTables(mPivotTable)
                        If objpivottable IsNot Nothing Then
                            Dim sourceDataRange As Range = objwrksheet2.Range(mSourceRange)
                            Dim cache As PivotCache = mWorkBook.PivotCaches.Create(SourceType:=XlPivotTableSourceType.xlDatabase, SourceData:=sourceDataRange)
                            objpivottable.ChangePivotCache(cache)
                            objpivottable.RefreshTable()
                            mRetval = "Successful"
                        Else
                            mRetval = "Pivot open failed"
                        End If
                    Else
                        mRetval = "Worksheet open failed"
                    End If

预期结果应该是在两个数据透视表的更改数据源之后,切片器报告连接应继续在列表中显示两个数据透视表名称。

1 个答案:

答案 0 :(得分:0)

这是通用的VBA方法:

通常可以通过向工作簿添加新的PivotTable.SourceData来更改PivotCache。但是,如果切片器中包含此数据透视表,则必须先通过SlicerCache.PivotTables.RemovePivotTable()取消选中其报表连接。

如果更改了多个数据透视表的源数据,则所有包含的数据透视表都基于相同的PivotCache,则只能再次在切片器中重新分配其报表连接。

因此,在更改第一个数据透视表的源数据之后,您必须为所有其他数据透视表“重用”其新的数据透视表缓存。可以通过设置PivotTable.CacheIndex来完成此“重用”,只要其他枢纽分析表使用与第一个枢纽分析表相同的枢纽域(或它们的子集)即可。

注释:要使用以下代码,首先需要启用切片器的所有报告连接(因为SlicerCache.PivotTables仅返回选中的报告连接)。

Private Sub ChangeAllPivotSources()
    Dim objSlicerCache As SlicerCache
    Dim objPivotTable As PivotTable
    Dim objPivotTables() As PivotTable
    Dim i As Long

    ' get the slicercache, e. g. via its first pivottable:
    Set objPivotTable = ActiveWorkbook.Sheets(1).PivotTables(1)
    Set objSlicerCache = objPivotTable.Slicers(1).SlicerCache

    ' dimension array with all pivottable objects of the slicercache
    ReDim objPivotTables(1 To objSlicerCache.PivotTables.Count)

    ' remove all pivottables from slicer's report connections
    For i = objSlicerCache.PivotTables.Count To 1 Step -1
        Set objPivotTables(i) = objSlicerCache.PivotTables(i)
        objSlicerCache.PivotTables.RemovePivotTable objPivotTables(i)
    Next i

    ' create new pivotcache based on a new range for the first pivottable,
    ' use this pivotcache for all other pivottables also
    For i = 1 To UBound(objPivotTables)
        If i = 1 Then
            objPivotTables(i).ChangePivotCache ActiveWorkbook.PivotCaches.Create( _
                SourceType:=xlDatabase, _
                SourceData:=ActiveWorkbook.Sheets(3).Range("A1").CurrentRegion)
        Else
            objPivotTables(i).CacheIndex = objPivotTables(1).PivotCache.Index
        End If
    Next i

    ' reassign the report connections again
    For i = 1 To UBound(objPivotTables)
        objSlicerCache.PivotTables.AddPivotTable objPivotTables(i)
    Next i

End Sub