VBA - Worksheet Change

jack109

Board Regular
Joined
May 10, 2020
Messages
63
Office Version
  1. 365
Platform
  1. Windows
Hi

I want to adjust the following code so that it only happens when B4 is changed and not all cells in the worksheet.

VBNA
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.ScreenUpdating = False
Worksheets("BREAKDOWN").Range("B4").Copy Worksheets("DATA").Range("B2")
Application.ScreenUpdating = True
End Sub

Many thanks
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Try:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("B4")) is Nothing Then
    Application.ScreenUpdating = False
    Worksheets("BREAKDOWN").Range("B4").Copy Worksheets("DATA").Range("B2")
    Application.ScreenUpdating = True
End If
End Sub
 
Upvote 0
Hello,
You can add a condition like so
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address(False, False, xlA1)<> "B4" Then Exit Sub
Application.ScreenUpdating = False
Worksheets("BREAKDOWN").Range("B4").Copy Worksheets("DATA").Range("B2")
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Hello,
You can add a condition like so
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address(False, False, xlA1)<> "B4" Then Exit Sub
Application.ScreenUpdating = False
Worksheets("BREAKDOWN").Range("B4").Copy Worksheets("DATA").Range("B2")
Application.ScreenUpdating = True
End Sub
One major difference between your code and mine - your code will only work if cell B4 is the ONLY cell updated.
Mine will work if cell B4 is ANY of the cells updated.

For example, if you copy cell B1 to B2:B10 (so 9 cells are updated at once, cell B4 being one of them), my code will fire, and yours will not.
 
Upvote 0
Yes indeed i wrote it with this exact idea in mind, as author wrote "it only happens when B4 is changed". But i'm not native speaker, i might have misunderstood.
Anyway, better like this, with your precision OP knows what to choose. (y)
 
Upvote 0
Yeah, the question is how/when "B4 is changed".
If only one cell is ever manually updated at a time, your code would work fine.
However, if they are using copy/paste, where they could be updating many cells at once (including "B4"), your code would not run.
Mine takes into account that possibility and will work in both instances.

That is the thing to remember about "Worksheet_Change" event procedures - when they are fired, it does not always mean just one cell was updated; a whole range of cells may have been updated at once.
 
Upvote 0
I agree with you 100%, and i understand the limitations that the solution i wrote brings. Indeed i wrote it with this very specific case (copy paste of a bigger range) in mind.

In my experience, there are also many cases in which you'd prefer not to fire the VBA event when you copy/paste a bunch of data. I think it's good we proposed the two options and that you took the time to describe very clearly the difference between them, so that OP can choose wisely.
 
Upvote 0
In my experience, there are also many cases in which you'd prefer not to fire the VBA event when you copy/paste a bunch of data. I think it's good we proposed the two options and that you took the time to describe very clearly the difference between them, so that OP can choose wisely.
Yes, a common way way to do that (to not fire the code if a multiple cells are changed at once), is to add this line to the very top of your code:
VBA Code:
'Exit code (don't run) if multiple cells are updated at once
If Target.CountLarge > 1 Then Exit Sub
 
Upvote 0
Thanks both, I was indeed looking for the code to ONLY run if/when B4 was updated.
 
Upvote 0
Thanks both, I was indeed looking for the code to ONLY run if/when B4 was updated.
Then either solution would work for that (as long as B4 would never be updated with other cells at the same time).
 
Upvote 0

Forum statistics

Threads
1,226,017
Messages
6,188,441
Members
453,474
Latest member
th9r

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