Help me Guys. VBA code to Compare a column of two worksheets and updating the unmatched records

Jabes7

New Member
Joined
Jul 13, 2012
Messages
12
Hi all,

Help me a VBA CODE. I have two sheets and the sample design is

Sheet1 Sheet2
ID Name sal ID Sal
100 aaa 1000 100 1000
101 bbb 2000 101 2000
102 ccc 3000 102 3000
103 ddd 4000
104 eee 5000
105 fff 6000



Now i need to compare ID COLUMN(unique column) of two Sheets and find the unmatched records.

And then Update the unmatched records into Sheet2

Similarly it should look like

Sheet1 Sheet2
ID Name sal ID Sal
100 aaa 1000 100 1000
101 bbb 2000 101 2000
102 ccc 3000 102 3000
103 ddd 4000 103 4000
104 eee 5000 104 5000
105 fff 6000 105 6000


Thanks in ADVANCE :)
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Maybe:

Code:
Sub Jabes7()

Dim lr As Long
Dim x As Range

lr = Cells(Rows.Count, 1).End(xlUp).Row

Columns("D:D").Insert shift:=xlToRight

With Range("D2:D" & lr)

    .Formula = "=VLOOKUP(A2,Sheet2!$A$2:$B$4,1,FALSE)"
    .Value = .Value
    
End With

For Each x In Range("D2:D" & lr)

    If x.Text = "#N/A" Then
    
        x.Offset(, -3).Copy Sheets("Sheet2").Range("A" & Rows.Count).End(3)(2)
        x.Offset(, -1).Copy Sheets("Sheet2").Range("B" & Rows.Count).End(3)(2)
    End If
    
Next x

Columns("D:D").Delete shift:=xlToLeft

    

End Sub
 
Upvote 0

Forum statistics

Threads
1,221,526
Messages
6,160,341
Members
451,638
Latest member
MyFlower

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