Update Pivot Table Filter Based on Value in Another Cell

BigBadBarry

New Member
Joined
Oct 23, 2013
Messages
5
Hi all (second thread this morning!),

I have managed to use some VB code (in Sheet2) to update a Pivot Table filter based on a value in my sheet (cell F1), pieced together from this forum.

What I can't do though is get F1 to reference another sheet (i.e. ='Sheet1'!C2) and update automatically. I have the VB in the worksheet with the Pivot table on, and obviously it doesn't run when I change cell C2 in Sheet 1.

I've tried to solve it myself using various sources and code in Sheet 1 or the module as well, but I am stuck. Any help would be greatly appreciated! The code in Sheet 2 is as follows:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim ws As Worksheet
Dim pt As PivotTable
Dim pi As PivotItem
Dim strField As String


strField = "Project"


On Error Resume Next
Application.EnableEvents = False
Application.ScreenUpdating = False


    If Target.Address = Worksheets("Sheet10").Range("F1").Address Then
        
        For Each ws In ThisWorkbook.Worksheets
            For Each pt In ws.PivotTables
                With pt.PageFields(strField)
                    For Each pi In .PivotItems
                        If pi.Value = Target.Value Then
                            .CurrentPage = Target.Value
                            Exit For
                        Else
                            .CurrentPage = "(All)"
                        End If
                    Next pi
                End With
            Next pt
        Next ws
    
    End If


Application.EnableEvents = True
Application.ScreenUpdating = True


End Sub
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Hi BigBadBarry,

Try pasting this example into the Sheet Code module of the Sheet that has the F1 Cell that you want the Pivot filters to be based upon.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim ws As Worksheet
    Dim pt As PivotTable
    Const strField As String = "Project"
    
    On Error Resume Next
    Application.EnableEvents = False
    Application.ScreenUpdating = False

    If Target.Address = "$F$1" Then
        For Each ws In ThisWorkbook.Worksheets
            For Each pt In ws.PivotTables
                With pt.PageFields(strField)
                    .ClearAllFilters
                    .CurrentPage = Target.Value
                End With
            Next pt
        Next ws
    End If

    Application.EnableEvents = True
    Application.ScreenUpdating = True
End Sub

If you're using Excel 2010 or later, consider using Slicers as another way to sync PivotTable filters.
 
Upvote 0
Thanks mate, that is perfect. I am using 2013 but Slicers didn't help me in this case - very long story but this solves my problems! Thanks you very much!
 
Upvote 0
Hi Jerry,

I am trying to run this code after pasting it in Sheet code module but its not happening. Is there anything else I need to enter in this code? How it will detect the target value from the required sheet?
 
Upvote 0
Hi rrocking,
Did you modify the code to match your field name in the place of "Project" and the cell with the desired value in place of "$F$1".

If that doesn't fix things try adding a statement to display a message box with the address of the changed (Target) cell.
That will help narrow down the potential causes of this not working.

Code:
   Msgbox Target.Address
   If Target.Address = "$F$1" Then
 
Upvote 0
Hi Jerry,

I'm hoping you might be able to help with my issue as well.

I am using this code in my dashboard to update pivot table filter for "month" which is a date value format mmm yy. But for some reason it will not work at all on this field. I'm guessing it is an issue with dates but I'm fairly new to VBA so not sure. I have tested the code on another text based filter "region" and it works absolutely fine on that but not for date. Any ideas?

Also the ultimate aim is actually to filter from two date options and provide the range so that all pivot tables are operating on the same date range filter i.e. From Mar 15 to Aug 15. However I have not been able to figure this step out, the above is as far as I have gotten.

Any help would be much appreciated.

As with bigbadbarry above users slicers are not an option as the dashboard is running normal graphs from a single date range on sheet1 and I have been asked to add to the dashboard with pivot tables - but the end user wants everything to operate from the two data validation drop downs on sheet 1.
 
Upvote 0

Forum statistics

Threads
1,215,234
Messages
6,123,773
Members
449,123
Latest member
StorageQueen24

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