Cell Change Macro

ManUBlueJay

Active Member
Joined
Aug 30, 2012
Messages
304
Office Version
  1. 2016
Platform
  1. Windows
Is there a way to have the worksheets change method monitor 2 cells and depending which one changes run code. Here is what I did but cannot seem to have the code run past the first if.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Not Intersect(Target, Range("B1")) Is Nothing Then
Range("A7") = "B1"
Else
If Not Intersect(Target, Range("B2")) Is Nothing Then
Range("A7") = "B2"

End If
End If

End Sub
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Hello,

You could test following:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
If Target.Address = "$B$1" Or Target.Address = "$B$2" Then Range("A7") = Target
End Sub

Hope this will help
 
Upvote 0
Thank you, but it does not seem to work. I stepped it through and it does not get to the second line
 
Upvote 0
Hi again,

What do you call ' the second line ' ...?

May be misunderstood your objective ...
 
Upvote 0
The second line of your code.
It skips out after the first line
 
Upvote 0
That seems to imply that it is seeing multiple cells being updated at once, so it is exiting the code, as per this line:
Code:
If Target.Count > 1 Then Exit Sub
How exactly is the data being updated?
Are they the result of formulas?
Are you copy/pasting multiple cells at once?
Or do you have merged cells that you are updating?

It maybe helpful to walk us through an actual example of exactly what you are doing.
 
Last edited:
Upvote 0
Sorry I was trying to keep it simple.
B1 and B2 are part of a pivot table controlled by 2 seperate slicers.
If B1 changes I want a given code to run, If B2 changes I want a different code to run.
 
Upvote 0
If they are calculated values, you cannot use Worksheet_Change on it. That event procedure runs when values in specified cells are manually updated.
If the values change due to calculations, that would be a Worksheet_Calculate event procedure. The tricky part about using that it runs whenever ANY cell on the sheet is re-calculated. It cannot tell which cell was re-calculated.
 
Upvote 0
How about code using the PivotTableChangeSync. I tried but didn't mnanage
 
Upvote 0
How about code using the PivotTableChangeSync. I tried but didn't mnanage
I am afraid I probably cannot help you with that. I very seldom work with Pivot Tables (just not something I have to work with in my line of work).
Most stuff that would fall into that category we end up using database programs for instead of Excel.

Had I known that Pivot Table/Slicers were involved, I probably would have not put my two cents in.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,429
Messages
6,124,840
Members
449,193
Latest member
MikeVol

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