我可以动态更改Power Pivot数据透视表的当前页面过滤器还是通过更改切片器过滤器

时间:2019-10-14 12:01:50

标签: excel vba

我尝试了两种不同的方法来解决此问题:

我有一个基于Power Pivot数据模型创建的数据透视表。我试图为当前页面过滤器创建代码,以根据具有销售员输入的单元格列表进行更改。

我的代码在这里:

Sub Macro1()
'
' Macro1 Macro
'

Dim rng As Range
Dim txt As String

Set rng = Application.Range("Rapport!M3:M4")
Dim cel As Range
For Each cel In rng.Cells
    With cel
    txt = cel.Value
    ActiveSheet.PivotTables("PivotTable1").PivotFields( _
    "[Stage BudgetLine].[SalespersonCode]. 
   [SalespersonCode]").ClearAllFilters _
        ActiveSheet.PivotTables("PivotTable1").PivotFields( _
    "[Stage BudgetLine].[SalespersonCode].[SalespersonCode]").CurrentPage = _
    "[Stage BudgetLine].[SalespersonCode].&[" & txt & "]"
    '"[Closed Cases].[Closed Date Week End].&[" & FilterDate & "]"
'        Debug.Print .Address & ":" & .Value
End With
Next cel

End Sub

然后,我尝试了一个切片器,如果我对SalesPersonsCode进行硬编码,但当我尝试使其变得灵活时,它就可以工作:

Sub Macro2()
'
' Macro2 Macro
'

'
Dim rng As Range
Dim txt As String

Set rng = Application.Range("Rapport!M3:M4")
Dim cel As Range
For Each cel In rng.Cells
    With cel
      txt = cel.Value



 ActiveWorkbook.SlicerCaches("Slicer_SalespersonCode"). _ 
VisibleSlicerItemsList 
= _
'        Array("[Stage BudgetLine].[SalespersonCode].&[AC]")
   Array("[Stage BudgetLine].[SalespersonCode].&[" & txt & "]")
  ActiveWorkbook.SlicerCaches("Slicer_SalespersonCode").ClearManualFilter


 ActiveWorkbook.SlicerCaches("Slicer_SalespersonCode"). _
 VisibleSlicerItemsList 
= _
        Array( _
    "[Stage BudgetLine].[SalespersonCode].&[AGE]")
ActiveWorkbook.SlicerCaches("Slicer_SalespersonCode").ClearManualFilter


End With
Next cel
End Sub

我试图阅读与该主题相关的问题。据我所知,有人说,我必须能够在过滤器中选择多个元素。 我可以理解,我必须将其作为数组来处理,但对我而言没有任何作用。

VBA在应将txt作为销售人员代码的动态更改插入时停止运行

0 个答案:

没有答案