Conditional Copying

futureancient

Board Regular
Joined
Dec 8, 2008
Messages
76
This should be simple, but I can't think of how to do this..

Outline -

I want to copy data from one sheet to another sheet if the values in a column match up between the two. These values change or get altered, so a simple vlookup wont do, as the copied data will also change - once copied, I need it to remain that way.

Detail -

In sheet1 I have column C with 1,2,3,4,5 etc. In Sheet2, there is column A also with 1,2,3,4,5... In columns D and E there is data, I want to copy this data to sheet1 columns G and H on the corresponding rows with the same numbers. ie Sheet1 C35 = 1, Sheet2 A5 = 1, copy Sheet2 D5 and E5, to Sheet1 G35 and H35.

After each copy, I will run a macro that will change which rows display 1,2,3,4,5... and so, the data it corresponds with will change. How do I make the data stick to where it is copied to in Sheet1, after the macro is run and the data changes.

Thanks for your help.
 
Last edited:

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Try this code:

Sub copydata()
Dim ws1 As Worksheet, ws2 As Worksheet, RowCounter As Integer, LastRow As Integer
Set ws1 = ActiveWorkbook.Worksheets("Sheet1")
Set ws2 = ActiveWorkbook.Worksheets("Sheet2")
'Get last row in Sheet2
LastRow = ws2.Cells(ws1.Rows.Count, 1).End(xlUp).Row

For RowCounter = 1 To LastRow
If ws1.Cells(RowCounter, 3) = ws2.Cells(RowCounter, 1) Then
'Values match so copy the data to sheet 1
ws2.Cells(RowCounter, 4).Copy Destination:=ws1.Cells(RowCounter, 7)
ws2.Cells(RowCounter, 5).Copy Destination:=ws1.Cells(RowCounter, 8)​
End If​
Next RowCounter
End Sub
 
Upvote 0
Thanks for that, but it doesn't seem to be working. I run the macro and I get a lot of quickly jumping numbers..

It's not necessary to get to the last row, simply to copy across sheets the data that currently relates to 1,2,3,4 etc.

I will then alter what rows these are in and their order, they then need copying again. Essentially I could be filling up columns G and H in a random sequence. The problem of copying over rows that have already been done won't be an issue, since they will never arrive on those rows again due to how I'm altering the values...
 
Upvote 0

Forum statistics

Threads
1,224,595
Messages
6,179,799
Members
452,943
Latest member
Newbie4296

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