将Excel切片器和多个数据透视表与VBA

时间:2018-04-15 16:29:30

标签: vba excel-vba pivot-table slicers excel

我正在尝试组合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

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

所有数据透视表共享许多常用字段,例如主数字键。 我已经尝试创建一个单独的查询来将所有数据合并到一个表中,因此我可以使用Slicers来导航不同数据透视表中的数据,但是,因为唯一查询对于单个集合具有不同数量的寄存器主键,最终结果很混乱。

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

谢谢大家。

嗯,事情并没有像我想象的那么顺利。 该过程适用于静态数据库。我的意思是,我不改变原始数据集。 但是数据集必须改变,因为数据表将由查询来完成。 第一个错误,即apear是运行时错误5。 所以我研究了一下,并了解应该重置pivotcaches以便多次运行该过程。 我找到了一个代码来执行此操作,但现在我遇到以下错误:运行时错误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

我犯了错误?

0 个答案:

没有答案