Compare two columns of two sheets and insert row if new data found

Itzybell

New Member
Joined
Jul 15, 2019
Messages
7
Hey everyone! This is my first time posting here :)
I've been scrolling through the forum for a solution to my problem but none seems to work!

I've just recently started doing macros and
I have a macro that would compare two excel sheets Col A(Sheet 1 and Sheet 2) and output the new data found in sheet 2 to the bottom of sheet 1 data:

Sheet 1:
No.Name
A100Obj1
A300Obj3
A400Obj4

<tbody>
</tbody>
Sheet 2:
No.Name
A100Obj1
A200Obj2
A300Obj3
A400Obj4
A500Obj5
A600Obj6

<tbody>
</tbody>

OUTPUT(Sheet 1):
No.Name
A100Obj1
A300Obj3
A400Obj4
A200Obj2
A500 Obj5
A600Obj6

<tbody>
</tbody>
The problem is that now I wish to insert the new data to Sheet 1 by inserting new rows in between Sheet 1's data after comparison automatically so I get the overview of where the new data is:
No.Name
A100Obj1
A200Obj2
A300Obj3
A400Obj4
A500 Obj5
A600Obj6

<tbody>
</tbody>

Is there a macro that will be able to do this automatically?
Thank you so much for your help!
I've been really troubled over this.
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Simplest way is to sort sheet1 after updating it.
 
Upvote 0
Try:
Code:
Sub CompareCols()
    Application.ScreenUpdating = False
    Dim ws1 As Worksheet, ws2 As Worksheet, i As Long, v1 As Variant, v2 As Variant, LastRow As Long
    Set ws1 = Sheets("Sheet1")
    Set ws2 = Sheets("Sheet2")
    v1 = ws1.Range("A2", ws1.Range("A" & Rows.Count).End(xlUp)).Value
    v2 = ws2.Range("A2", ws2.Range("A" & Rows.Count).End(xlUp)).Resize(, 2).Value
    Application.ScreenUpdating = False
    With CreateObject("Scripting.Dictionary")
        For i = 1 To UBound(v1, 1)
            If Not .Exists(v1(i, 1)) Then
                .Add v1(i, 1), Nothing
            End If
        Next i
        For i = 1 To UBound(v2, 1)
            If Not .Exists(v2(i, 1)) Then
                ws1.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0) = v2(i, 1)
                ws1.Cells(Rows.Count, "B").End(xlUp).Offset(1, 0) = v2(i, 2)
            End If
        Next i
    End With
    LastRow = ws1.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    With ws1.Sort
        .SortFields.Clear
        .SortFields.Add Key:=Range("A2:A" & LastRow), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .SetRange Range("A1:B" & LastRow)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hi Fluff!

I may have to rephrase my question,
My Col A is actually Id number that is not of an ascending order,
and col B is actually a description:

Sheet 1:
No.Name
A10256Jane
A23767Mary
A34732John

<colgroup><col><col></colgroup><tbody>
</tbody>

Sheet 2:
No.Name
A10256Jane
A34763Tom
A23767Mary
A34732John
A84578Jerry
A45734Rose

<colgroup><col><col></colgroup><tbody>
</tbody>

OUTPUT (Sheet1):

No.Name
A10256Jane
A23767Mary
A34732John
A34763Tom
A84578Jerry
A45734Rose

<colgroup><col><col></colgroup><tbody>
</tbody>

------------------------------
Desired output (Sheet 1):
No.Name
A10256Jane
A34763Tom
A23767Mary
A34732John
A84578Jerry
A45734Rose

<colgroup><col width="64" span="2" style="width:48pt"> </colgroup><tbody>
</tbody>

 
Upvote 0
Do you just want to copy over cols A & B, or are there other columns that need to be copied?
 
Upvote 0
If it is just the 1st 2 columns, try
Code:
Sub Itzybell()
   Dim Ary As Variant
   Dim i As Long, j As Long
   
   Ary = Sheets("Sheet2").Range("A1").CurrentRegion.Value2
   With Sheets("Sheet1")
      j = .Range("A" & Rows.Count).End(xlUp).Row
      For i = UBound(Ary) To 2 Step -1
         If .Cells(j, 1).Value = Ary(i, 1) Then
            j = j - 1
         Else
            Rows(j + 1).Insert
            .Cells(j + 1, 1).Resize(, 2).Value = Array(Ary(i, 1), Ary(i, 2))
         End If
      Next i
   End With
End Sub
 
Upvote 0
Hi Mumps!

I realized that your macro would sort and group the duplicates together.

The macro I would need have to insert new rows from Sheet 2 to Sheet 1 without messing up the data if it is the same data in Col A (Sheet 1 and 2)

E.g:

Sheet 1:
No.
Name
A10256Jane
A23767Mary
A34732John

<colgroup><col width="64" style="width:48pt" span="2"> </colgroup><tbody>
</tbody>

Sheet 2:
No.
Name
A10256Jane
A34763Tom
A23767Mary

<colgroup><col width="64" style="width:48pt" span="2"> </colgroup><tbody>
</tbody>

OUTPUT (Sheet1):
No.
Name
A10256Jane
A34763Tom
A23767Mary
A34732John

<colgroup><col width="64" style="width:48pt" span="2"> </colgroup><tbody>
</tbody>
------------------------
When comparing Column A, once a data is different it will automatically be inserted into Sheet1 to the row.

Thank you! :)
 
Upvote 0
Hi Fluff,

The code worked for a sample data set that had small amount of edited rows,
However when I import it into my data file which had about 50+ new data sets in Sheet 2 it wasn't able to insert into the respective places in Sheet 1 and the new data ends up at the bottom of data of Sheet 1 again.

Is there a condition that I had to follow?..

On the question if other columns are to be copied,I would actually need to map over the data from Column J to S as well.
But the unique identifier would still be Column A.

Thank you!
 
Upvote 0
Hi I have similar problem


XYURLA BSolution
12www.1.com23
23www.2.com12www.1.com
34www.3.com45
45www.4.com34




<colgroup><col width="64" span="6" style="width:48pt"> </colgroup><tbody>
</tbody>



If Column A and B Matches with Column X and Y then URL Next to X and Y should be inserted in Solutions. one example is shown . can you help

Thanks well in advance
 
Upvote 0
@jpmayekar
Please start a thread of your own, rather than "hijacking" somebodyelse's thread.
Thanks
 
Upvote 0

Forum statistics

Threads
1,213,534
Messages
6,114,186
Members
448,554
Latest member
Gleisner2

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