using VBA to filter a pivot table based on a cells value

JoeyH

New Member
Joined
Feb 9, 2022
Messages
9
Office Version
  1. 365
Platform
  1. Windows
Hello, I have been searching for hours trying to get this code to work. I am trying to get a pivot table to filter based on the value within a specific cell that will change often. When using the code below I get a run-time error '1004': "unable to set the currentPage property of the PivotField class". I have tried a few things I have found online with no luck. Any and all help is greatly appreciated.

--------------------------------------------------------------------------

Private Sub Worksheet_Change(ByVal Target As Range)

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

Dim pt As PivotTable
Dim Field As PivotField
Dim NewCat As String

Set pt = Worksheets("DATA_SOURCE").PivotTables("TESTING")
Set Field = pt.PivotFields("[Table2].[ID #].[ID #]")

NewCat = Worksheets("DATA_SOURCE").Range("G41").Value

With Field
.ClearAllFilters
.CurrentPage = NewCat
pt.RefreshTable
End With

End Sub
 
Try another option
Note: Before testing, update the pivot table values.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  If Target.CountLarge > 1 Then Exit Sub
  If Intersect(Target, Range("G41")) Is Nothing Then Exit Sub
  Application.ScreenUpdating = False
  With ActiveSheet.PivotTables("TESTING").PivotFields("[Table2].[ID #].[ID #]")
    .ClearAllFilters
    .CurrentPage = Target.Value
  End With
End Sub
 
Upvote 0

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Hello @JoeyH, give this a try.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  Dim pTable As PivotTable
  Dim pField As PivotField
  Dim pItem As PivotItem
  Dim n As Long
 
  Dim Target As Range
  Set Target = Range("G41")
 
  If Target.CountLarge > 1 Then Exit Sub
  If Intersect(Target, Range("G41")) Is Nothing Then Exit Sub
 
  Application.ScreenUpdating = False
 
  Set pTable = Worksheets("DATA_SOURCE").PivotTables("TESTING")
  Set pField = pTable.PivotFields("[Table2].[ID #].[ID #]")
   
   pField.ClearAllFilters

   On Error Resume Next
       pField.CurrentPageName = "[Table2].[ID #].&[" & Range("G41") & "]"
   On Error GoTo 0
   
End Sub
Unfortinulatly this did not work either.
 
Upvote 0
Did you try post #11?
you can share your book on google drive or dropbox.
 
Upvote 0
I was also waiting until you replied to Dante's post #11. "This did not work" is not exactly a lot to go on.
If @DanteAmor's option doesn't work then sharing the workbook as he suggested would be ideal, we don't need a lot of actual data but a few unedited ID numbers would be good to leave in the table.

Since we seem to be in a different time zone and to keep it moving:-
  • I had to change Dante's .CurrentPage line to the below to make it work on my test data.
    .CurrentPageName = "[Table2].[ID #].&[" & Target.Value & "]"
  • Is your code in the Code page of the Sheet "DATA_SOURCE" ?
  • Please confirm G41 and the Pivot are both on the sheet DATA_SOURCE
  • In post #6 you refer to E41, does E41 have any relevance or can we ignore that ?
  • What does not work mean when you tried my code ? Did you get a VBA error or did it not do anything ?
    Same for when you tried post #11.
  • Is G41 part of a merged cell ?
If you force it manually is at least one of the formulas I have here TRUE.

20220213 VBA Pivot Table Filter.xlsm
EFGHIJK
39
40=G41=H43=""&G41=""&H43
41Filter Value111111FALSETRUE
42
43ID #111111
44
45Sum of AMOUNT
46CATERGORYTotal
47COG10
48Grand Total10
49
DATA_SOURCE
Cell Formulas
RangeFormula
I40:J40I40=FORMULATEXT(I41)
I41I41=G41=H43
J41J41=""&G41=""&H43
 
Upvote 0

Forum statistics

Threads
1,214,968
Messages
6,122,506
Members
449,089
Latest member
RandomExceller01

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