VBA Update Pivot when cell value changes

NormChart55

New Member
Joined
Feb 22, 2022
Messages
44
Office Version
  1. 2016
Platform
  1. Windows
Hello All,

I am trying to have a pivot table filter change to be what is in another cell. We use a template that has a SKU list and the user can use a select macro to copy the SKU into another more in depth analysis page which pulls a bunch of other data. But we have to then manually change the pivot table filter. I am wanting the filter to update to what is in the other cell. I found a simple one online, but seems to not actually do anything despite to me what looks to be correct references. After the users select the SKU, it copies that into the other sheet cell D668 and that is the one I want the filter to also adjust to. I am not sure why this does not seem to do anything. any thoughts?

PivotTable6 is the pivot
Product SKU is the field
D668 is the cell that is changing.

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

'This line stops the worksheet updating on every change, it only updates when cell
'D668 is touched
If Intersect(Target, Range("D668:D669")) Is Nothing Then Exit Sub

'Set the Variables to be used
Dim pt As PivotTable
Dim Field As PivotField
Dim NewCat As String

'Here you amend to suit your data
Set pt = Worksheets("JDE Template").PivotTables("PivotTable6")
Set Field = pt.PivotFields("Product SKU")
NewCat = Worksheets("JDE Template").Range("D668").Value

'This updates and refreshes the PIVOT table
With pt
Field.ClearAllFilters
Field.CurrentPage = NewCat
pt.RefreshTable
End With

End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
I should note - the pivot table filter options are in CELL B3 and that is what I am wanting to update based on D668

1686157426470.png
 
Upvote 0
I tried a simpler version and just tried to record a copy paste into that cell, however that only hard codes a specific references in there. it does not paste what was copied each new time and seems to always add to the filter and not just the new number

VBA Code:
Range("D668").Select
Selection.CopyActiveSheet.PivotTables("PivotTable6").PivotFields("Product SKU").CurrentPage _
= "(All)"
With ActiveSheet.PivotTables("PivotTable6").PivotFields("Product SKU")
.PivotItems("CLVZAP003345").Visible = False
.PivotItems("CLVZAP003343").Visible = True
End With

 
Upvote 0
solved with this code from youtube video

VBA Code:
Dim a As String
Dim pt As PivotTable

ThisWorkbook.Worksheets("Sheet 1").Activate

a = Worksheets("Sheet 1").Cells(668, 4).Value

For Each pt In ActiveSheet.PivotTables
    With pt.PivotFields("Product SKU")
    .ClearAllFilters
    .CurrentPage = a
   
    End With
   
Next
 
Upvote 0
Solution

Forum statistics

Threads
1,223,894
Messages
6,175,250
Members
452,623
Latest member
Techenthusiast

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