Copy cell value via VBA if specific cell is changed

Schturman

Board Regular
Joined
May 28, 2022
Messages
63
Office Version
  1. 2019
Platform
  1. Windows
Hi to all
I need some help to change the code that I use to copy value...
This code I use:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range

 If Not Intersect(Target, Range("B2")) Is Nothing Then
  Set rng = Range("C2")
  rng.Value = Range("A2")
    Else
    If Not Intersect(Target, Range("B3")) Is Nothing Then
     Set rng = Range("C3")
     rng.Value = Range("A2")
     End If
  End If

End Sub
1680284474739.png


If I do any changes to B2, it will copy A2 to C2
If I do any changes to B3, it will copy A2 to C3
And so on...

When I add new row (for example insert value to B4), I need to update my code that will do the same copy to C4
Can someone help me to change the code that will do what I want without every time updating VBA code ?
Thanks
 
Target.Address is the cell address of the cell in column B that you edit. If you offset(0,1) you are referencing a cell in the same row, one cell to the right of B, which is empty. Do you see the solution?

Multiply by the Target.Value, not the offset.
Target.Offset(0, 1) = Range("A2") * Target.Value
It's really cool, Thanks !
I get the same result by changed this:
VBA Code:
If Not Intersect(Target, Range("B:B")) Is Nothing Then Target.Offset(0, 1) = Range("A2") * Target.Offset(0, 0)
But now I know, instead Offset I can use Target.Value

Thank you very much !
 
Upvote 0

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
It's really cool, Thanks !
I get the same result by changed this:
VBA Code:
If Not Intersect(Target, Range("B:B")) Is Nothing Then Target.Offset(0, 1) = Range("A2") * Target.Offset(0, 0)
But now I know, instead Offset I can use Target.Value

Thank you very much !
Well, not quite. You have mixed up a few things (concepts).

"Target" is just a range object/variable.

"Target.Value" returns the value of that range.
However, adding the ".Value" is really not necessary.
If you leave it off, like "Target", it will default to the value.

Adding ".Offset(0,0)" is just saying from your starting range, move 0 rows down and 0 row to the right (in other words, stay where you are!).

So, the shortest form to write it everything after your "Then" would be:
VBA Code:
Target.Offset(0, 1) = Range("A2") * Target
The fully qualified equivalent version would be:
VBA Code:
Target.Offset(0, 1).Value = Range("A2").Value * Target.Value

Adding "Offset(0,0)" serves no purpose whatsoever.

I hope that makes sense!
 
Upvote 0
Well, not quite. You have mixed up a few things (concepts).

"Target" is just a range object/variable.

"Target.Value" returns the value of that range.
However, adding the ".Value" is really not necessary.
If you leave it off, like "Target", it will default to the value.

Adding ".Offset(0,0)" is just saying from your starting range, move 0 rows down and 0 row to the right (in other words, stay where you are!).

So, the shortest form to write it everything after your "Then" would be:
VBA Code:
Target.Offset(0, 1) = Range("A2") * Target
The fully qualified equivalent version would be:
VBA Code:
Target.Offset(0, 1).Value = Range("A2").Value * Target.Value

Adding "Offset(0,0)" serves no purpose whatsoever.

I hope that makes sense!
WoW, thank you for explanation !!! I learned some cool stuff ! :)
 
Upvote 0
Hi.
I want to ask another question about this code.
For example if I use Text in the column B, I get error of code and this OK, because it can't calculate:
Code:
Range("A2") * Target

I changed code a little bit to:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Avoid error if user selects & deletes multiple rows
If Target.Columns.Count > 1 Then Exit Sub

 If Not Intersect(Target, Range("B:B")) Is Nothing Then
   If Target = Target.Text Then
   Application.EnableEvents = False
   Target.Offset(0, 1) = ""
   Else
   Application.EnableEvents = False
   Target.Offset(0, 1) = Range("A2") * Target
   End If
   
 End If
 Application.EnableEvents = True 'error handle

End Sub

Now it working good.

And a questions:
1. Is there a more elegant way to do this or this one is ok?

2. I noticed about something that didn't saw before because didn't tried this... When I drag value (text or number) in a column B more than one cell - I get error of code. I can copy value one by one without errors, but dragging cause error. Can it be fixed somehow ?
Thanks
 
Upvote 0
maybe try this instead
If Target.Columns.Count > 1 OR Target.Rows.Count > 1 then Exit Sub

Not sure what you mean by "if I use Text in the column B, I get error of code".
Maybe test if the cell contains a number and if not don't calculate?
Don't know if there's a better way or not.
 
Upvote 0
If Target.Columns.Count > 1 OR Target.Rows.Count > 1 then Exit Sub
If you change it to this:
VBA Code:
If Target.CountLarge > 1 Then Exit Sub
it will exit the sub whenever multiple cells are changed at once (regardless if there are columns or rows).
This is handy to handle things like large deletions or inserts of rows & columns.
 
Upvote 0
Thanks to both of you!
Ok, I tested both of yours variants, both of them working perfect.
But both of them not calculate if I drag numbers in column B. I understand because it do
Code:
Exit Sub
When I drag numbers one by one, it calculate perfectly.
It possible to do calculating when I drag more than one cell ? For example I write 20 euro in cell B4 and drag it till B8, I expect to get calculated price in C5,C6,C7,C8 (If I dragging one by one I get this...)

Not sure what you mean by "if I use Text in the column B, I get error of code".
Maybe test if the cell contains a number and if not don't calculate?

About text... Some times I can use text in column B instead the numbers. For example If I don't need to calculate, I can write in column B something like: "Stable price in NIS" and in column C, will write the price manually. That mean if I use Text' it should not calculate, this is a reason why I changed a part of the code to this and asked about more elegant way:
VBA Code:
   If Target = Target.Text Then
   Application.EnableEvents = False
   Target.Offset(0, 1) = ""
   Else
   Application.EnableEvents = False
   Target.Offset(0, 1) = Range("A2") * Target
   End If
1681406569374.png


In the picture you can see I dragged the 21 euro down and it not calculate because of
Code:
Exit Sub
If it not possible to change dragging behavior, it's also ok. Like I said before I noticed about error code by mistake when dragged a few cells :) Now error fixed and it's ok.
 
Upvote 0
What is the point of this line here?
VBA Code:
If Target = Target.Text Then
What are you trying to accomplish with that?
 
Upvote 0
@Joe4, thanks for continuing to educate. I didn't know about that property.
Not being an Excel expert I'm going to say that you're going to have to loop over the selected cells if you want to drag values down, then validate that each cell value is a number and if not, don't multiply.
 
Upvote 0

Forum statistics

Threads
1,223,702
Messages
6,173,959
Members
452,539
Latest member
delvey

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