简体   繁体   English

Excel 2013 VBA数据透视表筛选器将更改,但不会显示值

[英]Excel 2013 VBA Pivot Table Filters will change but values wont show

I am working on a dashboard that has 4 Cascading Combo Boxes, where one filters the next which filters the next. 我正在使用具有4个层叠组合框的仪表板,其中一个过滤下一个,第二个过滤下一个。 They are connected to a linked cell with a named range. 它们连接到具有命名范围的链接单元格。

On another worksheet I have Pivot Table that is connected to a Pivot Chart on the same page as my Combo Boxes. 在另一个工作表上,我的数据透视表与“组合框”位于同一页上,并连接到数据透视表。

Long story short, as one combobox changes a filter on my pivot table changes, which changes the pivot chart. 长话短说,因为一个组合框更改了我的数据透视表上的过滤器,从而更改了数据透视表。

I have done this two ways, one way performs half the time but they both end up the same way. 我已经完成了这两种方式,一种方式执行一半的时间,但是两者都以相同的方式结束。 I can change the filters but the pivot table will not show values. 我可以更改过滤器,但数据透视表不会显示值。

在此处输入图片说明

I have pieced this together and mended it to fit my needs. 我将其拼凑在一起并对其进行了修整以满足我的需求。

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

I believe I figured it out!, on to the next issue. 我相信我已经解决了!,继续下一期。 The solution was to add 解决方案是添加

ThisWorkbook.Sheets("Pivot").PivotTables("PT1").ClearAllFilters before the comboboxes. 组合框之前的ThisWorkbook.Sheets("Pivot").PivotTables("PT1").ClearAllFilters

Amended code below. 修改后的代码如下。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM