I'm trying to write a macro to filter a pivot table based off a cell value.
I've adapted the code from this SO post: How to control Excel PIVOT tables from cell values with VBA
And this is mine currently
However when I go to run the macro, the macro list pops up every time like in the attached image.
I've a feeling I'm missing something basic or don't have the right settings for it.
I've adapted the code from this SO post: How to control Excel PIVOT tables from cell values with VBA
And this is mine currently
VBA Code:
Sub Worksheet_SelectionChange(ByVal Target As Range)
'This line stops the worksheet updating on every change, 'it only updates when cell'B4 or B5 is touched
If Intersect(Target, Worksheet.Sheet3.Range("E7")) Is Nothing Then Exit Sub
'Set the Variables to be usedDim pt As PivotTable
Dim FieldRegion As PivotField
Dim NewRegion As String
Set pt = Worksheets("Sheet5").PivotTables("PivotTable2")
Set FieldRegion = pt.PivotFields("Player")
NewRegion = Worksheet.Sheet3.Range("E7")
'This updates and refreshes the PIVOT table
With pt
FieldRegion.ClearAllFilters
FieldRegion = NewRegion
pt.RefreshTable
EndWith
EndSub
However when I go to run the macro, the macro list pops up every time like in the attached image.
I've a feeling I'm missing something basic or don't have the right settings for it.