VBA - Script to search and replace values in rows between sheets based on criteria

RobertHamberg

New Member
Joined
Jan 11, 2018
Messages
34
Office Version
  1. 365
I want ascript to search for the values in column A in sheet 1 and try to find a match in column A in Sheet 2. If a match is found I want the script to copy the values in column F-J in sheet 1 and add those values to column F-J in sheet 2 on the matching rows. Anyone able to help? :)

Sheet 1[TABLE="width: 500"]
<tbody>[TR]
[TD][TABLE="class: cke_show_border"]
<tbody>[TR]
[TD][/TD]
[TD="width: 64"]A[/TD]
[TD="width: 126"]B[/TD]
[TD="width: 73"]C[/TD]
[TD="width: 73"]D[/TD]
[TD="width: 64"]E[/TD]
[TD="width: 147"]F[/TD]
[TD="width: 64"]G[/TD]
[TD="width: 64"]H[/TD]
[TD="width: 64"]I[/TD]
[TD="width: 64"]J[/TD]
[/TR]
[TR]
[TD="width: 64"]1[/TD]
[TD="width: 64"]Name[/TD]
[TD="width: 126"]Owner[/TD]
[TD="width: 73"]Start[/TD]
[TD="width: 73"]End[/TD]
[TD="width: 64"]Time (H)[/TD]
[TD="width: 147"]Team[/TD]
[TD="width: 64"]R1[/TD]
[TD="width: 64"]%[/TD]
[TD="width: 64"]R2[/TD]
[TD="width: 64"]%[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Test2[/TD]
[TD]Owner2[/TD]
[TD="align: right"]2018-01-01[/TD]
[TD="align: right"]2018-12-31[/TD]
[TD="align: right"]200[/TD]
[TD]Team2[/TD]
[TD]Resurs2[/TD]
[TD="align: right"]100[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]0[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]Test3[/TD]
[TD]Owner3[/TD]
[TD="align: right"]2018-01-01[/TD]
[TD="align: right"]2018-12-31[/TD]
[TD="align: right"]300[/TD]
[TD]Team3[/TD]
[TD]Resurs3[/TD]
[TD="align: right"]100[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]0[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]Test5[/TD]
[TD]Owner5[/TD]
[TD="align: right"]2018-01-01[/TD]
[TD="align: right"]2018-12-31[/TD]
[TD="align: right"]500[/TD]
[TD]Team5[/TD]
[TD]Resurs5[/TD]
[TD="align: right"]100[/TD]
[TD="align: right"]0[/TD]
[TD="align: right"]0[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]Test6[/TD]
[TD]Owner6[/TD]
[TD="align: right"]2018-01-01[/TD]
[TD="align: right"]2018-12-31[/TD]
[TD="align: right"]600[/TD]
[TD]Team6[/TD]
[TD]Resurs6[/TD]
[TD="align: right"]75[/TD]
[TD]Resurs3[/TD]
[TD="align: right"]25[/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]Test8[/TD]
[TD]Owner8[/TD]
[TD="align: right"]2018-01-01[/TD]
[TD="align: right"]2018-12-31[/TD]
[TD="align: right"]800[/TD]
[TD]Team8[/TD]
[TD]Resurs8[/TD]
[TD="align: right"]50[/TD]
[TD]Resurs7[/TD]
[TD="align: right"]50[/TD]
[/TR]
[TR]
[TD]7[/TD]
[TD]Test10[/TD]
[TD]Owner10[/TD]
[TD="align: right"]2018-01-01[/TD]
[TD="align: right"]2018-12-31[/TD]
[TD="align: right"]1 000[/TD]
[TD]Team10[/TD]
[TD]Resurs10[/TD]
[TD="align: right"]50[/TD]
[TD]Resurs9[/TD]
[TD="align: right"]50[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[/TR]
</tbody>[/TABLE]
Sheet 2[TABLE="width: 500"]
<tbody>[TR]
[TD][TABLE="class: cke_show_border"]
<tbody>[TR]
[TD][/TD]
[TD="width: 64"]A[/TD]
[TD="width: 126"]B[/TD]
[TD="width: 73"]C[/TD]
[TD="width: 73"]D[/TD]
[TD="width: 64"]E[/TD]
[TD="width: 147"]F[/TD]
[TD="width: 64"]G[/TD]
[TD="width: 64"]H[/TD]
[TD="width: 64"]I[/TD]
[TD="width: 64"]J[/TD]
[/TR]
[TR]
[TD="width: 64"]1[/TD]
[TD="width: 64"]Name[/TD]
[TD="width: 126"]Owner[/TD]
[TD="width: 73"]Start[/TD]
[TD="width: 73"]End[/TD]
[TD="width: 64"]Time (H)[/TD]
[TD="width: 147"]Team[/TD]
[TD="width: 64"]R1[/TD]
[TD="width: 64"]%[/TD]
[TD="width: 64"]R2[/TD]
[TD="width: 64"]%[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Test1[/TD]
[TD]Owner1[/TD]
[TD="align: right"]2018-01-01[/TD]
[TD="align: right"]2018-12-31[/TD]
[TD="align: right"]100[/TD]
[TD][/TD]
[TD][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]Test2[/TD]
[TD]Owner2[/TD]
[TD="align: right"]2018-01-01[/TD]
[TD="align: right"]2018-12-31[/TD]
[TD="align: right"]200[/TD]
[TD][/TD]
[TD][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]Test3[/TD]
[TD]Owner3[/TD]
[TD="align: right"]2018-01-01[/TD]
[TD="align: right"]2018-12-31[/TD]
[TD="align: right"]300[/TD]
[TD][/TD]
[TD][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]Test4[/TD]
[TD]Owner4[/TD]
[TD="align: right"]2018-01-01[/TD]
[TD="align: right"]2018-12-31[/TD]
[TD="align: right"]400[/TD]
[TD][/TD]
[TD][/TD]
[TD="align: right"][/TD]
[TD][/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]Test5[/TD]
[TD]Owner5[/TD]
[TD="align: right"]2018-01-01[/TD]
[TD="align: right"]2018-12-31[/TD]
[TD="align: right"]500[/TD]
[TD][/TD]
[TD][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD]7[/TD]
[TD]Test6[/TD]
[TD]Owner6[/TD]
[TD="align: right"]2018-01-01[/TD]
[TD="align: right"]2018-12-31[/TD]
[TD="align: right"]600[/TD]
[TD][/TD]
[TD][/TD]
[TD="align: right"][/TD]
[TD][/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD]8[/TD]
[TD]Test7[/TD]
[TD]Owner7[/TD]
[TD="align: right"]2018-01-01[/TD]
[TD="align: right"]2018-12-31[/TD]
[TD="align: right"]700[/TD]
[TD][/TD]
[TD][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD]9[/TD]
[TD]Test8[/TD]
[TD]Owner8[/TD]
[TD="align: right"]2018-01-01[/TD]
[TD="align: right"]2018-12-31[/TD]
[TD="align: right"]800[/TD]
[TD][/TD]
[TD][/TD]
[TD="align: right"][/TD]
[TD][/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD]10[/TD]
[TD]Test9[/TD]
[TD]Owner9[/TD]
[TD="align: right"]2018-01-01[/TD]
[TD="align: right"]2018-12-31[/TD]
[TD="align: right"]900[/TD]
[TD][/TD]
[TD][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD]11[/TD]
[TD]Test10[/TD]
[TD]Owner10[/TD]
[TD="align: right"]2018-01-01[/TD]
[TD="align: right"]2018-12-31[/TD]
[TD="align: right"]1 000[/TD]
[TD][/TD]
[TD][/TD]
[TD="align: right"][/TD]
[TD][/TD]
[TD="align: right"][/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Untested, but try this
Code:
Sub CopyCols()

   Dim Cl As Range
   Dim Sht1 As Worksheet
   Dim Sht2 As Worksheet
   
   Set Sht1 = Sheets("Sheet1")
   Set Sht2 = Sheets("Sheet2")
   
   With CreateObject("scripting.dictionary")
      For Each Cl In Sht1.Range("A2", Sht1.Range("A" & Rows.Count).End(xlUp))
         If Not .exists(Cl.Value) Then .Add Cl.Value, Cl
      Next Cl
      For Each Cl In Sht2.Range("A2", Sht2.Range("A" & Rows.Count).End(xlUp))
         If .exists(Cl.Value) Then .Item(Cl.Value).EntireRow.Copy Cl
      Next Cl
   End With

End Sub
 
Upvote 0
Works like a charm, so thankfull for all the help you've provided!

Untested, but try this
Code:
Sub CopyCols()

   Dim Cl As Range
   Dim Sht1 As Worksheet
   Dim Sht2 As Worksheet
   
   Set Sht1 = Sheets("Sheet1")
   Set Sht2 = Sheets("Sheet2")
   
   With CreateObject("scripting.dictionary")
      For Each Cl In Sht1.Range("A2", Sht1.Range("A" & Rows.Count).End(xlUp))
         If Not .exists(Cl.Value) Then .Add Cl.Value, Cl
      Next Cl
      For Each Cl In Sht2.Range("A2", Sht2.Range("A" & Rows.Count).End(xlUp))
         If .exists(Cl.Value) Then .Item(Cl.Value).EntireRow.Copy Cl
      Next Cl
   End With

End Sub
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,326
Members
452,635
Latest member
laura12345

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