Execute Macro Based on Cell Change -> without using Enter key

mosesthetank

New Member
Joined
Nov 30, 2013
Messages
16
The below macro creates a filter based on the value in A2. I want it to execute every time A2 changes. A2 is a cell reference to another sheet (pivot table). The only way the filter currently updates is if I press Enter in A2. I have tried writing this several different ways but they all require pressing Enter.

Can the below code be be written so as to refresh the filter as A2 changes without using Enter?

Code:
[FONT=Arial]Private Sub Worksheet_Change(ByVal Target As Range)[/FONT][FONT=Arial]Dim KeyCells As Range[/FONT]
[FONT=Arial]Set KeyCells = Range("A2")[/FONT]
[FONT=Arial]
[/FONT]
[FONT=Arial]If Not Application.Intersect(<wbr>KeyCells, Range(Target.Address)) _[/FONT]
[FONT=Arial]           Is Nothing Then[/FONT]
[FONT=Arial]
[/FONT]
[FONT=Arial]Set Target = Target.Cells(1, 1)[/FONT]
[FONT=Arial]If Not Intersect(Target, Range("A2")) Is Nothing Then[/FONT]
[FONT=Arial]Range("B2").CurrentRegion.<wbr>AutoFilter field:=2, Criteria1:=Target.Value[/FONT]
[FONT=Arial]End If[/FONT]
[FONT=Arial]
[/FONT]
[FONT=Arial]
[/FONT]
[FONT=Arial]End If[/FONT]
[FONT=Arial]
[/FONT]
[FONT=Arial]End Sub[/FONT]
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
put the code into the worksheet calculate event instead of (or as well as) the worksheet change event
 
Last edited:
Upvote 0
I change it to Worksheet_Calculate and got a compile error:

Procedure declaration does not match description of event or procedure having the same name.

Any guidance would be appreciated.
 
Upvote 0
The worksheet calculate event does not include the the "Target" parameter so you either need to detect whether A2 has changed by keeping a record of it or just recalculate the filter every time using code like this:

Code:
Private Sub Worksheet_Calculate()

Range("B2").CurrentRegion.AutoFilter field:=2, Criteria1:=Cells(1, 1).Value


End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,834
Messages
6,121,874
Members
449,056
Latest member
ruhulaminappu

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