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

ectiger86

Board Regular
Joined
Aug 27, 2014
Messages
67
I am working on a dashboard that has 4 Cascading Combo Boxes, where one filters the next which filters the next. 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 combo box 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.

(table with filters applied but no values showing)
YVKYfwx.jpg




I have pieced this together and mended it to fit my needs.

Code:
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")
       
    
    
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


'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
pt.PivotCache.Refresh


Application.ScreenUpdating = True

End Sub




If there is anything you see that can improve the code such as a better way to do something or speed it up feel free to let me know!



Any ideas?
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
When you say value do not update, I assume the filter do (so if you check see the filters change with your combos) but not the output of the pivot

I see you refresh the pivot already, which was basically my only idea to explain that case but let's try, just to be sure


Code:
ActiveSheet.Calculate
ActiveWorkbook.RefreshAll
 
Upvote 0
Do i need to do away with any of the refreshers I currently have on there now?

Also are the placed in the proper spot in my code?
 
Upvote 0
SOLVED - Excel 2013 VBA Pivot Table Filters will change but values wont show

I have figured it out! It is working like a champ so far, lets hope it holds together! I added this piece of code before the filters
Code:
ThisWorkbook.Sheets("Pivot").PivotTables("PT1").ClearAllFilters


On to the next issue!

Amended code below

Code:
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
 
Upvote 0
Re: SOLVED - Excel 2013 VBA Pivot Table Filters will change but values wont show

I have figured it out! It is working like a champ so far, lets hope it holds together! I added this piece of code before the filters
Code:
ThisWorkbook.Sheets("Pivot").PivotTables("PT1").ClearAllFilters


On to the next issue!

Amended code below

Code:
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

Thanks for letting us know, I will remember that one
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top