VBA: Check two lists a merge missing rows

FryGirl

Well-known Member
Joined
Nov 11, 2008
Messages
1,366
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have two sheets, Sheet1 and Sheet2. Need to merge what's additional on Sheet2 into Sheet1.

The column to look for additional data is column B and when a non-match is found, it should be added to Sheet1.

In this example, the new records from Sheet2 are (in column B): Record 4, Recored E, and Record q.

Before (Sheet1)
[TABLE="class: grid"]
<tbody>[TR]
[TD][/TD]
[TD]
A​
[/TD]
[TD]
B​
[/TD]
[TD]
C​
[/TD]
[/TR]
[TR]
[TD]
1​
[/TD]
[TD]
Hdr 1​
[/TD]
[TD]
Hdr 2​
[/TD]
[TD]
Hdr 3​
[/TD]
[/TR]
[TR]
[TD]
2​
[/TD]
[TD]
First​
[/TD]
[TD]
Record 2​
[/TD]
[TD]
$11,907.00​
[/TD]
[/TR]
[TR]
[TD]
3​
[/TD]
[TD]
First​
[/TD]
[TD]
Record 3​
[/TD]
[TD]
$10,175.00​
[/TD]
[/TR]
[TR]
[TD]
4​
[/TD]
[TD]
First​
[/TD]
[TD]
Record 1​
[/TD]
[TD]
$2,620.00​
[/TD]
[/TR]
[TR]
[TD]
5​
[/TD]
[TD]
Second​
[/TD]
[TD]
Record C​
[/TD]
[TD]
$10,544.00​
[/TD]
[/TR]
[TR]
[TD]
6​
[/TD]
[TD]
Second​
[/TD]
[TD]
Record D​
[/TD]
[TD]
$9,795.00​
[/TD]
[/TR]
[TR]
[TD]
7​
[/TD]
[TD]
Second​
[/TD]
[TD]
Record A​
[/TD]
[TD]
$8,447.00​
[/TD]
[/TR]
[TR]
[TD]
8​
[/TD]
[TD]
Second​
[/TD]
[TD]
Record B​
[/TD]
[TD]
$7,885.00​
[/TD]
[/TR]
[TR]
[TD]
9​
[/TD]
[TD]
Third​
[/TD]
[TD]
Record n​
[/TD]
[TD]
$10,282.00​
[/TD]
[/TR]
[TR]
[TD]
10​
[/TD]
[TD]
Third​
[/TD]
[TD]
Record m​
[/TD]
[TD]
$9,166.00​
[/TD]
[/TR]
[TR]
[TD]
11​
[/TD]
[TD]
Third​
[/TD]
[TD]
Record o​
[/TD]
[TD]
$6,197.00​
[/TD]
[/TR]
[TR]
[TD]
12​
[/TD]
[TD]
Third​
[/TD]
[TD]
Record p​
[/TD]
[TD]
$1,084.00​
[/TD]
[/TR]
</tbody>[/TABLE]

After (Sheet1)
[TABLE="class: grid"]
<tbody>[TR]
[TD][/TD]
[TD]
E​
[/TD]
[TD]
F​
[/TD]
[TD]
G​
[/TD]
[/TR]
[TR]
[TD]
1​
[/TD]
[TD]
Hdr 1​
[/TD]
[TD]
Hdr 2​
[/TD]
[TD]
Hdr 3​
[/TD]
[/TR]
[TR]
[TD]
2​
[/TD]
[TD]
First​
[/TD]
[TD]
Record 2​
[/TD]
[TD]
$11,907.00​
[/TD]
[/TR]
[TR]
[TD]
3​
[/TD]
[TD]
First​
[/TD]
[TD]
Record 3​
[/TD]
[TD]
$10,175.00​
[/TD]
[/TR]
[TR]
[TD]
4​
[/TD]
[TD]
First​
[/TD]
[TD]
Record 1​
[/TD]
[TD]
$2,620.00​
[/TD]
[/TR]
[TR]
[TD]
5​
[/TD]
[TD]
First​
[/TD]
[TD]
Record 4​
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]
6​
[/TD]
[TD]
Second​
[/TD]
[TD]
Record C​
[/TD]
[TD]
$10,544.00​
[/TD]
[/TR]
[TR]
[TD]
7​
[/TD]
[TD]
Second​
[/TD]
[TD]
Record D​
[/TD]
[TD]
$9,795.00​
[/TD]
[/TR]
[TR]
[TD]
8​
[/TD]
[TD]
Second​
[/TD]
[TD]
Record A​
[/TD]
[TD]
$8,447.00​
[/TD]
[/TR]
[TR]
[TD]
9​
[/TD]
[TD]
Second​
[/TD]
[TD]
Record B​
[/TD]
[TD]
$7,885.00​
[/TD]
[/TR]
[TR]
[TD]
10​
[/TD]
[TD]
Second​
[/TD]
[TD]
Record E​
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]
11​
[/TD]
[TD]
Third​
[/TD]
[TD]
Record n​
[/TD]
[TD]
$10,282.00​
[/TD]
[/TR]
[TR]
[TD]
12​
[/TD]
[TD]
Third​
[/TD]
[TD]
Record m​
[/TD]
[TD]
$9,166.00​
[/TD]
[/TR]
[TR]
[TD]
13​
[/TD]
[TD]
Third​
[/TD]
[TD]
Record o​
[/TD]
[TD]
$6,197.00​
[/TD]
[/TR]
[TR]
[TD]
14​
[/TD]
[TD]
Third​
[/TD]
[TD]
Record p​
[/TD]
[TD]
$1,084.00​
[/TD]
[/TR]
[TR]
[TD]
15​
[/TD]
[TD]
Third​
[/TD]
[TD]
Record q​
[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]


Source Data (Sheet2)
[TABLE="class: grid"]
<tbody>[TR]
[TD][/TD]
[TD]
A​
[/TD]
[TD]
B​
[/TD]
[/TR]
[TR]
[TD]
1​
[/TD]
[TD]
Hdr 1​
[/TD]
[TD]
Hdr 2​
[/TD]
[/TR]
[TR]
[TD]
2​
[/TD]
[TD]
First​
[/TD]
[TD]
Record 1​
[/TD]
[/TR]
[TR]
[TD]
3​
[/TD]
[TD]
First​
[/TD]
[TD]
Record 2​
[/TD]
[/TR]
[TR]
[TD]
4​
[/TD]
[TD]
First​
[/TD]
[TD]
Record 3​
[/TD]
[/TR]
[TR]
[TD]
5​
[/TD]
[TD]
First​
[/TD]
[TD]
Record 4​
[/TD]
[/TR]
[TR]
[TD]
6​
[/TD]
[TD]
Second​
[/TD]
[TD]
Record A​
[/TD]
[/TR]
[TR]
[TD]
7​
[/TD]
[TD]
Second​
[/TD]
[TD]
Record B​
[/TD]
[/TR]
[TR]
[TD]
8​
[/TD]
[TD]
Second​
[/TD]
[TD]
Record C​
[/TD]
[/TR]
[TR]
[TD]
9​
[/TD]
[TD]
Second​
[/TD]
[TD]
Record D​
[/TD]
[/TR]
[TR]
[TD]
10​
[/TD]
[TD]
Second​
[/TD]
[TD]
Record E​
[/TD]
[/TR]
[TR]
[TD]
11​
[/TD]
[TD]
Third​
[/TD]
[TD]
Record m​
[/TD]
[/TR]
[TR]
[TD]
12​
[/TD]
[TD]
Third​
[/TD]
[TD]
Record n​
[/TD]
[/TR]
[TR]
[TD]
13​
[/TD]
[TD]
Third​
[/TD]
[TD]
Record o​
[/TD]
[/TR]
[TR]
[TD]
14​
[/TD]
[TD]
Third​
[/TD]
[TD]
Record p​
[/TD]
[/TR]
[TR]
[TD]
15​
[/TD]
[TD]
Third​
[/TD]
[TD]
Record q​
[/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Code:
Sub t()
Dim sh1 As Worksheet, sh2 As Worksheet, c As Range
Set sh1 = Sheets("Sheet1")
Set sh2 = Sheets("Sheet2")
    For Each c In sh2.Range("B2", sh2.Cells(Rows.Count, 2).End(xlUp))
        If Application.CountIf(sh1.Range("B:B"), c.Value) = 0 Then
            c.EntireRow.Copy sh1.Cells(Rows.Count, 1).End(xlUp)(2)
        End If
    Next
sh1.UsedRange.Sort sh1.Range("A1"), xlAscending, , sh1.Range("B1"), xlAscending, Header:=xlYes
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,249
Messages
6,171,031
Members
452,374
Latest member
keccles

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