control 2 pivot tables selection based on cell value

The VBA code below will allow me to enter a value into cell H6 which will automatically change my pivot table selection. I would now like to add to the code so H6 will also change the selection of a second pivot table. The second pivot table will be exactly the same as the first but will obviously be named PivotTable2.

I'm not sure how to edit the code. Could some one be so kind as to edit and re-paste?

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
 
'This line stops the worksheet updating on every change, it only updates when cell
'H6 or H7 is touched
If Intersect(Target, Range("H6:H7")) 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("Sheet1").PivotTables("PivotTable1")
Set Field = pt.PivotFields("Category")
NewCat = Worksheets("Sheet1").Range("H6").Value
 
'This updates and refreshes the PIVOT table
With pt
Field.ClearAllFilters
Field.CurrentPage = NewCat
pt.RefreshTable
End With
 
End Sub

Thanks Mike
mikes6058Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rgonzo1971Commented:
HI,

pls try

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  
 'This line stops the worksheet updating on every change, it only updates when cell
 'H6 or H7 is touched
 If Intersect(Target, Range("H6:H7")) 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("Sheet1").PivotTables("PivotTable1")
 Set Field = pt.PivotFields("Category")
 NewCat = Worksheets("Sheet1").Range("H6").Value
  
 'This updates and refreshes the PIVOT table
 With pt
 Field.ClearAllFilters
 Field.CurrentPage = NewCat
 pt.RefreshTable
 End With
 
  'Here you amend to suit your data
 Set pt = Worksheets("Sheet1").PivotTables("PivotTable2")
 Set Field = pt.PivotFields("Category")
 NewCat = Worksheets("Sheet1").Range("H6").Value
  
 'This updates and refreshes the PIVOT table
 With pt
 Field.ClearAllFilters
 Field.CurrentPage = NewCat
 pt.RefreshTable
 End With
  
 End Sub

Open in new window

Regards
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mikes6058Author Commented:
perfect
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.