1

我正在尝试结合 Excel 数据透视表切片器来控制多个数据透视表形成不同的数据表。

我发现一个函数可以检测并记录切片器的活动选择:

Public Function SlicerSelections(Slicer_Name As String)
FblSlicerSelections = ""

Dim i As Integer

With ActiveWorkbook.SlicerCaches(Slicer_Name)
    For i = 1 To .SlicerItems.Count
        If .SlicerItems(i).Selected Then
            SlicerSelections = SlicerSelections & " " & .SlicerItems(i).Value
    End If
    Next i
End With
End Function

现在我想使用这些函数的结果来改变其他数据透视表的数据透视字段。

所有数据透视表都共享许多公共字段,例如主键。我已经尝试创建一个查询以将所有数据组合到一个表中,因此我可以使用切片器来浏览不同数据透视表中的数据,但是,由于唯一查询对于单个集合具有不同数量的寄存器主键,最终结果是混乱的。

如何使用 SlicerSelections 函数的结果来更改数据透视表的过滤器参数(数据透视字段)并刷新它,以便我可以使用第一个数据透视表切片器和宏来命令电子表格中的所有数据透视表?

谢谢你们。

好吧,事情并没有我想象的那么顺利。该过程非常适用于静态数据库。我的意思是,我不会更改原始数据集。但是数据集必须更改,因为数据表将由查询来完成。出现的第一个错误是运行时错误 5。所以我进行了一些研究并了解到应该重置数据透视缓存以便多次运行该过程。我找到了执行此操作的代码,但现在我面临以下错误:运行时错误 1004 - 应用程序定义或对象定义错误。

这是代码:

Sub Atualiza_Din()
'Dim pvtf As PivotField
Call PivotCacheReset

' Definição das variáveis de controle
Set pvtf_Dias_cen = ActiveSheet.PivotTables("TDDias").PivotFields("Número")
Set pvtf_Dias_per = ActiveSheet.PivotTables("TDDias").PivotFields("PER_Nome")
Set pvtf_Dias_ref = ActiveSheet.PivotTables("TDDias").PivotFields("REF_Nome")
Set pvtf_Dias_tpu = ActiveSheet.PivotTables("TDDias").PivotFields("TPU_Nome")
Set pvtf_Dias_upr = ActiveSheet.PivotTables("TDDias").PivotFields("UPR_Nome")

Set pvtf_Prod_cen = ActiveSheet.PivotTables("TDProd").PivotFields("Número")
Set pvtf_Prod_per = ActiveSheet.PivotTables("TDProd").PivotFields("PER_Nome")
Set pvtf_Prod_ref = ActiveSheet.PivotTables("TDProd").PivotFields("REF_Nome")
Set pvtf_Prod_tpu = ActiveSheet.PivotTables("TDProd").PivotFields("TPU_Nome")
Set pvtf_Prod_upr = ActiveSheet.PivotTables("TDProd").PivotFields("UPR_Nome")

cen = SlicerSelections("SegmentaçãodeDados_Número1") 'Identifica o cenário selecionado
per = SlicerSelections("SegmentaçãodeDados_PER_Nome1") 'Identifica o período selecionado
ref = SlicerSelections("SegmentaçãodeDados_REF_Nome1") 'Identifica a refinaria selecionada
tpu = SlicerSelections("SegmentaçãodeDados_TPU_Nome1") 'Identifica o processo selecionado
upr = SlicerSelections("SegmentaçãodeDados_UPR_Nome1") 'Identifica a unidade selecionada
'MsgBox (ref)

pvtf_Dias_cen.CurrentPage = cen
pvtf_Dias_per.CurrentPage = per
pvtf_Dias_ref.CurrentPage = ref
pvtf_Dias_tpu.CurrentPage = tpu
pvtf_Dias_upr.CurrentPage = upr

pvtf_Prod_cen.CurrentPage = cen
pvtf_Prod_per.CurrentPage = per
pvtf_Prod_ref.CurrentPage = ref
pvtf_Prod_tpu.CurrentPage = tpu
pvtf_Prod_upr.CurrentPage = upr

End Sub

这是枢轴缓存重置:

Sub PivotCacheReset()
' When a data set is pivoted and then some of the underlying data is
'  removed, even after a refresh old removed values can still remain in the
'  pivot filter.  This macro changes the properties of all pivot tables in
'  the active workbook, to prevent missing items from appearing, or clear
'  items that have appeared. It also resets all pivot table caches in the
'  active workbook.

    Dim wb As Workbook
    Set wb = ActiveWorkbook

    Dim iWs As Worksheet
    Dim pvt As PivotTable
    Dim iProtectState As Boolean

    ' Loop through each worksheet.
    For Each iWs In wb.Worksheets

        ' If the worksheet is protected, unprotect it. Remember
        '  it was protected so that it can be re-protected later.
        iProtectState = False                    ' Reset variable that remembers if each sheet is protected.
        If iWs.ProtectContents = True Then
            ' Worksheet is protected.
            iWs.Unprotect                        ' Unprotect the worksheet.
            iProtectState = True                 ' Remember that this worksheet was protected.
        End If

        ' Loop through each pivot table in the sheet.
        For Each pvt In iWs.PivotTables
            pvt.PivotCache.MissingItemsLimit = xlMissingItemsNone ' Don't allow missing items in the pivot table filters.
            pvt.PivotCache.Refresh               ' Reset the pivot table cache including any missing items.
        Next pvt

        ' If the worksheet was originally protected, re-protect it.
        If iProtectState = True Then iWs.Protect

    Next iWs

End Sub

我在哪里犯错?

4

0 回答 0