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.
 

Some videos you may like

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
42,953
Office Version
365
Platform
Windows
Simplest way is to sort sheet1 after updating it.
 

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
9,200
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
 

Itzybell

New Member
Joined
Jul 15, 2019
Messages
7
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>

 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
42,953
Office Version
365
Platform
Windows
Do you just want to copy over cols A & B, or are there other columns that need to be copied?
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
42,953
Office Version
365
Platform
Windows
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
 

Itzybell

New Member
Joined
Jul 15, 2019
Messages
7
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! :)
 

Itzybell

New Member
Joined
Jul 15, 2019
Messages
7
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!
 

jpmayekar

New Member
Joined
Oct 25, 2017
Messages
8
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
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
42,953
Office Version
365
Platform
Windows
@jpmayekar
Please start a thread of your own, rather than "hijacking" somebodyelse's thread.
Thanks
 

Watch MrExcel Video

Forum statistics

Threads
1,102,859
Messages
5,489,309
Members
407,686
Latest member
Chuck1960

This Week's Hot Topics

  • Timer in VBA - Stop, Start, Pause and Reset
    [CODE=vba][/CODE] Option Explicit Dim CmdStop As Boolean Dim Paused As Boolean Dim Start Dim TimerValue As Date Dim pausedTime As Date Sub...
  • how to updates multiple rows in muliselect listbox
    Hello everyone. I need help with below code. code is only chaning 1st row in mulitiselect list box. i know issue with code...
  • Delete Row from Table
    I am trying to delete a row from a table using VBA using a named range to find what I need to delete. My Range is finding the right cell. In the...
  • Assigning to a variable
    I have a for each block where I want to assign the value in column 5 of the found row to the variable Serv. [CODE=vba] For Each ws In...
  • Way to verify information
    Hi All, I don't know what to call this formula, and therefore can't search. I have a spreadsheet with information I want to reference...
  • Active Cell Address – Inactive Sheet
    How to use VBA to get the cell address of the active cell in an inactive worksheet and then place that cell address in a location on the current...
Top