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
ABCDEFGHIJ
1NameOwnerStartEndTime (H)TeamR1%R2%
2Test2Owner22018-01-012018-12-31200Team2Resurs210000
3Test3Owner32018-01-012018-12-31300Team3Resurs310000
4Test5Owner52018-01-012018-12-31500Team5Resurs510000
5Test6Owner62018-01-012018-12-31600Team6Resurs675Resurs325
6Test8Owner82018-01-012018-12-31800Team8Resurs850Resurs750
7Test10Owner102018-01-012018-12-311 000Team10Resurs1050Resurs950

<tbody>
</tbody>

<tbody>
</tbody>
Sheet 2
ABCDEFGHIJ
1NameOwnerStartEndTime (H)TeamR1%R2%
2Test1Owner12018-01-012018-12-31100
3Test2Owner22018-01-012018-12-31200
4Test3Owner32018-01-012018-12-31300
5Test4Owner42018-01-012018-12-31400
6Test5Owner52018-01-012018-12-31500
7Test6Owner62018-01-012018-12-31600
8Test7Owner72018-01-012018-12-31700
9Test8Owner82018-01-012018-12-31800
10Test9Owner92018-01-012018-12-31900
11Test10Owner102018-01-012018-12-311 000

<tbody>
</tbody>

<tbody>
</tbody>
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Hi & welcome to the board.
Are the col A values on sheet1 unique?
 
Upvote 0
Hi & welcome to the board.
Are the col A values on sheet1 unique?

Values in col A are unique in both sheet1 and sheet2. I have a script that selects certain rows in sheet1 and copy them to sheet2, then a script that adds values to rows in sheet2. Now I need a script that copy those values from sheet2 back to sheet1 on matching rows :)
 
Upvote 0
How about
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.Offset(, 5).Resize(, 5)
      Next Cl
      For Each Cl In Sht2.Range("A2", Sht2.Range("A" & Rows.Count).End(xlUp))
         If .exists(Cl.Value) Then Cl.Offset(, 5).Resize(, 5).Value = .Item(Cl.Value).Value
      Next Cl
   End With

End Sub
 
Upvote 0
Without loop

Code:
Sub CopyPaste()
    Dim LastRow     As Long
    Sheets("Sheet2").Select
    LastRow = Range("A" & Rows.Count).End(xlUp).Row
    Range("F2").Formula = "=IFERROR(VLOOKUP($A2,Sheet1!$A:$J,COLUMN(),0),"""")"
    Range("F2").Copy
    With Range("F2:J" & LastRow)
        .PasteSpecial xlPasteFormulas
        .Copy
        .PasteSpecial xlPasteValues
    End With
End Sub
 
Last edited:
Upvote 0
Or a slightly simpler (and corrected, missing a = sign) version vds1 code
Code:
Sub CopyPaste()
    Dim LastRow     As Long
    With Sheets("Sheet2")
      LastRow = .Range("A" & Rows.Count).End(xlUp).Row
      With .Range("F2:J" & LastRow)
         .Formula = "=IFERROR(VLOOKUP($A2,Sheet1!$A:$J,COLUMN(),0),"""")"
         .Value = .Value
      End With
   End With
End Sub
 
Upvote 0
I need help to re-write this script. I need it to do the following. Match value in column A in Sheet 1 with value in column A in Sheet 2, if match found I want the script to copy and replace the row on sheet 2 with the row from sheet 1.

I want it to work no matter the amount of columns and rows that contain data in sheet 1 and sheet 2.

Thanks in advance!

Sheet 1

A
B
C
D
E
F
G
H
I
J
1
Name
Owner
Start
End
Time (H)
Team
R1
%
R2
%
2
Test2
Owner2
2018-01-01
2018-12-31
200
Team2
Resurs2
100

<tbody>
</tbody>

<tbody>
</tbody>
Sheet 2

A
B
C
D
E
F
G
H
I
J
1
Name
Owner
Start
End
Time (H)
Team
R1
%
R2
%
2
Test1
Owner1
2018-01-01
2018-12-31
100
Team2
Resurs2
25Resurs275
3
Test2
Owner2

2018-01-01
2018-12-31
200
Team2

Resurs6
100Resurs40
4
Test3
Owner3
2018-01-01
2018-12-31
300
Team2
Resurs2
50Resurs150
5
Test4
Owner4
2018-01-01
2018-12-31
400
Team2
Resurs2
75Resurs325
6
Test5
Owner5
2018-01-01
2018-12-31
500
Team25
Resurs2
80Resurs620

<tbody>
</tbody>

<tbody>
</tbody>
 
Upvote 0
Maybe
Code:
Sub CopyPaste()
    Dim UsdRws As Long
    Dim UsdCols As Long
    With Sheets("Sheet2")
      UsdRws = .Range("A" & Rows.Count).End(xlUp).Row
      UsdCols = Sheets("Sheet1").Cells(1, Columns.Count).End(xlToLeft).Column
      With .Range("B2", .Cells(UsdRws, UsdCols))
         .Formula = "=IFERROR(VLOOKUP($A2,Sheet1!$1:$" & UsdRws & ",COLUMN(),0),"""")"
         .Value = .Value
      End With
   End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,648
Messages
6,120,726
Members
448,987
Latest member
marion_davis

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