Excel 2013 VBA数据透视表过滤器将更改但值不会显示

时间:2015-08-21 18:58:30

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

我正在制作一个带有4个Cascading Combo Boxes的仪表板,其中一个过滤下一个过滤下一个。它们连接到具有命名范围的链接单元格。

在另一张工作表上,我有一个数据透视表,它连接到与我的组合框相同页面上的数据透视表。

长话短说,因为一个组合框更改了我的数据透视表更改的过滤器,这会更改数据透视表。

我已经做了两种方式,一种方式执行了一半的时间,但它们都以同样的方式结束。我可以更改过滤器,但数据透视表不会显示值。

enter image description here

我把它拼凑在一起并修补它以满足我的需要。

Sub changeFilters()


Dim pt As PivotTable
Dim pf As PivotField
Dim pi As PivotItem
Dim wsChart As Worksheet
Dim wsPivot As Worksheet
Dim selCat  As Variant
Dim selSub  As Variant
Dim selLoc  As Variant
Dim selCust As Variant


Set wsChart = ThisWorkbook.Sheets("CHART")
Set wsPivot = ThisWorkbook.Sheets("Pivot")
Set pt = ThisWorkbook.Sheets("Pivot").PivotTables("PT1")
Set selCat = ThisWorkbook.Sheets("CHART").Range("selCat")
Set selSub = ThisWorkbook.Sheets("CHART").Range("selSub")
Set selLoc = ThisWorkbook.Sheets("CHART").Range("selLoc")
Set selCust = ThisWorkbook.Sheets("CHART").Range("selCust")



pt.ManualUpdate = True

Application.ScreenUpdating = False

For Each pi In pt.PivotFields("CATEGORY").PivotItems

Select Case pi.Name
    Case [selCat]
        pi.Visible = True
    Case Else
        pi.Visible = False
    End Select
Next pi


 For Each pi In pt.PivotFields("SUB-CATEGORY").PivotItems

Select Case pi.Name
    Case [selSub]
        pi.Visible = True
    Case Else
        pi.Visible = False
    End Select
Next pi



For Each pi In pt.PivotFields("LOCATION").PivotItems
Select Case pi.Name
    Case [selLoc]
        pi.Visible = True
    Case Else
        pi.Visible = False
    End Select
Next pi



For Each pi In pt.PivotFields("CUSTOMER").PivotItems
Select Case pi.Name
    Case [selCust]
        pi.Visible = True
    Case Else
        pi.Visible = False
    End Select
Next pi


'turn on automatic update / calculation in the Pivot Table

pt.ManualUpdate = False
pt.PivotCache.Refresh


Application.ScreenUpdating = True

End Sub

1 个答案:

答案 0 :(得分:0)

我相信我想出来了!到下一期。解决方案是添加

在组合框之前

ThisWorkbook.Sheets("Pivot").PivotTables("PT1").ClearAllFilters

以下修改后的代码。

Option Explicit
Sub changeFilters()


Dim pt As PivotTable
Dim pf As PivotField
Dim pi As PivotItem
Dim wsChart As Worksheet
Dim wsPivot As Worksheet
Dim selCat  As Variant
Dim selSub  As Variant
Dim selLoc  As Variant
Dim selCust As Variant


Set wsChart = ThisWorkbook.Sheets("CHART")
Set wsPivot = ThisWorkbook.Sheets("Pivot")
Set pt = ThisWorkbook.Sheets("Pivot").PivotTables("PT1")
Set selCat = ThisWorkbook.Sheets("CHART").Range("selCat")
Set selSub = ThisWorkbook.Sheets("CHART").Range("selSub")
Set selLoc = ThisWorkbook.Sheets("CHART").Range("selLoc")
Set selCust = ThisWorkbook.Sheets("CHART").Range("selCust")


Application.ScreenUpdating = False
pt.ManualUpdate = True

ThisWorkbook.Sheets("Pivot").PivotTables("PT1").ClearAllFilters



 For Each pi In pt.PivotFields("CATEGORY").PivotItems

Select Case pi.Name
    Case [selCat]
        pi.Visible = True
    Case Else
        pi.Visible = False
    End Select
Next pi


'Removes pivot items from pivot table except those cases defined below      (by looping through)
 For Each pi In pt.PivotFields("SUB-CATEGORY").PivotItems

Select Case pi.Name
    Case [selSub]
        pi.Visible = True
    Case Else
        pi.Visible = False
    End Select
Next pi



For Each pi In pt.PivotFields("LOCATION").PivotItems
Select Case pi.Name
    Case [selLoc]
        pi.Visible = True
    Case Else
        pi.Visible = False
    End Select
Next pi



For Each pi In pt.PivotFields("CUSTOMER").PivotItems
Select Case pi.Name
    Case [selCust]
        pi.Visible = True
    Case Else
        pi.Visible = False
    End Select
Next pi



'turn on automatic update / calculation in the Pivot Table

pt.ManualUpdate = False
 Application.ScreenUpdating = True




End Sub