Macro not recognising sub name

cc1987

New Member
Joined
Mar 4, 2016
Messages
18
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

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.
 

Attachments

  • Screenshot 2020-05-28 at 20.05.34.png
    Screenshot 2020-05-28 at 20.05.34.png
    71.8 KB · Views: 10

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
You do not "run" that code, it is designed to automatically run whenever you select a cell on the relevant worksheet.
Also the code should go in a worksheet module, not a standard module.
 
Upvote 0
You do not "run" that code, it is designed to automatically run whenever you select a cell on the relevant worksheet.
Also the code should go in a worksheet module, not a standard module.
Ahh thank you.
Haven't come across worksheet modules previously
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,224,747
Messages
6,180,712
Members
452,995
Latest member
isldboy

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