Why is this code not changing the filter of my pivot table?

vbaNumpty

Board Regular
Joined
Apr 20, 2021
Messages
171
Office Version
  1. 365
Platform
  1. Windows
I found this code online and I have tried to use it on my worksheet but the pivot table filter is not changing to match the value in cell

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim xPTable As PivotTable
    Dim xPFile As PivotField
    Dim xStr As String
    On Error Resume Next
    If Intersect(Target, Range("I3")) Is Nothing Then Exit Sub
    Application.ScreenUpdating = False
    Set xPTable = Sheet6.PivotTables("PivotTable3")
    Set xPFile = xPTable.PivotFields("Customer")
    xStr = Target.Text
    xPFile.ClearAllFilters
    xPFile.CurrentPage = xStr
    Application.ScreenUpdating = True
End Sub
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Do you have that code in the worksheet that you are monitoring I3 change?

If so, have you stepped through the code to see if it goes through the entire code?
 
Upvote 0
The critical lines are the following:-

1) Your new filter value needs to be in I3 on the activesheet and has to have been the last thing changed.
VBA Code:
If Intersect(Target, Range("I3")) Is Nothing Then Exit Sub

2) I would change Sheet6 to ActiveSheet or are you trying to change a pivot that is on a different sheet ?
also is PivotTable3 the name of you pivot table on that sheet
VBA Code:
Set xPTable = Sheet6.PivotTables("PivotTable3")

3) Just confirm that your pivot table filter field is called exactly Customer
VBA Code:
Set xPFile = xPTable.PivotFields("Customer")
 
Upvote 0
Do you have that code in the worksheet that you are monitoring I3 change?

If so, have you stepped through the code to see if it goes through the entire code?
My code is exiting the sub at

If Intersect(Target, Range("I3")) Is Nothing Then Exit Sub

Even though I3 is showing a value when hovered over
 
Upvote 0
The critical lines are the following:-

1) Your new filter value needs to be in I3 on the activesheet and has to have been the last thing changed.
VBA Code:
If Intersect(Target, Range("I3")) Is Nothing Then Exit Sub

2) I would change Sheet6 to ActiveSheet or are you trying to change a pivot that is on a different sheet ?
also is PivotTable3 the name of you pivot table on that sheet
VBA Code:
Set xPTable = Sheet6.PivotTables("PivotTable3")

3) Just confirm that your pivot table filter field is called exactly Customer
VBA Code:
Set xPFile = xPTable.PivotFields("Customer")

When I step through the code it is exiting the sub at :

VBA Code:
If Intersect(Target, Range("I3")) Is Nothing Then Exit Sub

Even though the range I3 is showing a value when hovered over.
 
Upvote 0
Did it enter the sub when you changed what was in I3. Target will be the cell that was changed.
 
Upvote 0
Did it enter the sub when you changed what was in I3. Target will be the cell that was changed.
So it turns out the logic was backwards, I added Not after the If and now it works, new problem however is that it is creating an infinite loop I believe. New code:

VBA Code:
    Dim pt As PivotTable
    Dim xPFile As PivotField
    Dim x As String

    If Not Intersect(Target, Range("I3")) Is Nothing Then Exit Sub
      
    Application.ScreenUpdating = False
   
    Set pt = ActiveSheet.PivotTables("PivotTable3")
    Set xPFile = pt.PivotFields("Customer")
    x = ActiveSheet.Range("I3").Text
    xPFile.ClearAllFilters
    xPFile.CurrentPage = x
   
    Application.ScreenUpdating = True

When the code reaches xPFile.ClearAllFilters it gets send back to the top of the code infinitely.
 
Upvote 0
Try starting the code with
VBA Code:
Application.EnableEvents = False
and ending it setting it back to True
 
Upvote 0
Did you put the false straight after the if statement ?
And the true as the very last line?
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,012
Messages
6,122,682
Members
449,091
Latest member
peppernaut

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