Comparing two sheets and adding in new data?

afc171

Board Regular
Joined
Jan 14, 2017
Messages
143
Office Version
  1. 2013
Platform
  1. Windows
Hi all,
I am trying to compare two sheets and copy the new data from 1 to another if its not there, this code only seems to copy the first 2 columns.
What do I need to change to include up to column I? Also is there a way to copy them to Compare sheet with a different colour to see what has been added?

VBA Code:
Sub Compare()
   Dim Ary As Variant
   Dim i As Long, j As Long
   
   Ary = Sheets("RECP1").Range("A1").CurrentRegion.Value2
   With Sheets("Compare")
      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

Thank you
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
I don't have any data to test this with but see if this does what you need:
The code in red are the changes

Rich (BB code):
Sub Compare()
   Dim Ary As Variant
   Dim i As Long, j As Long, zCol As Long
   
   Ary = Sheets("RECP1").Range("A1").CurrentRegion.Value2
   With Sheets("Compare")
      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
            For zCol = 1 To Columns("I").Column
                .Cells(j + 1, zCol).Value = Ary(i, zCol)
            Next zCol
            .Range(.Cells(j + 1, "A"), .Cells(j + 1, "I")).Interior.Color = vbYellow
         End If
      Next i
   End With
End Sub
 
Upvote 0
Solution
Alex can that code be modded to do it on 1 sheet...for example insert new rows into L:T and highlight if not in A:I ?
so its easier to see the differences. Thanks

Consumables Mastersheet V2.6 LC.xlsm
ABCDEFGHIJKLMNOPQRST
1productdescriptionlot_numberto_bin_numbermovement_quantityexpiry_datetransaction_typedatedWHSproductdescriptionlot_numberto_bin_numbermovement_quantityexpiry_datetransaction_typedatedWHS
2CHE0033SODIUM PYRUVATE2511572LCLAB284531/Jul/23RECP03/Oct/22LCCHE0033SODIUM PYRUVATE2511572LCLAB284531/Jul/23RECP03/Oct/22LC
3GAS0003LIQUID NITROGEN031022SHED568RECP03/Oct/22LCGAS0003LIQUID NITROGEN031022SHED568RECP03/Oct/22LC
4PRI0027Black Ink Cartridge031022LC264RECP03/Oct/22LC
5CHE0030L-GLUTAMINE 200MM2412068FC431/Jan/24RECP04/Oct/22LCCHE0030L-GLUTAMINE 200MM2412068FC431/Jan/24RECP04/Oct/22LC
6PKG0007MEDIUM DRY ICE BOX041022LC/P860RECP04/Oct/22LCPKG0007MEDIUM DRY ICE BOX041022LC/P860RECP04/Oct/22LC
7PKG0028VIAL BOX - M-410C041022LC/P8360RECP04/Oct/22LC
8CHE0029NON ESS AMINO ACIDS2512133LCLAB284528/Feb/24RECP05/Oct/22LCCHE0029NON ESS AMINO ACIDS2512133LCLAB284528/Feb/24RECP05/Oct/22LC
9CON0010175CM TC FLASKS2091013LC25229/Mar/27RECP05/Oct/22LCCON0010175CM TC FLASKS2091013LC25229/Mar/27RECP05/Oct/22LC
Sheet1
 
Upvote 0
I have logged off for the night but if you are using excel tables give me the table names.
 
Upvote 0
They are not in tables but if its easier, Table15 (left) and Table16 (right)
 
Upvote 0
Oops thanks for reminding me.
Your previous response indicated that you were not in fact using tables (I just thought the line colouring indicated you were).

If you are not using tables try the code below.

VBA Code:
Sub Compare()
    Dim Ary As Variant
    Dim i As Long, j As Long, zCol As Long
    Dim shtData As Worksheet
    Dim lrRECP1 As Long, lrComp As Long
    Dim rngRECP1 As Range, rngComp As Range
    
    Set shtData = ActiveSheet
    
    With shtData
        lrRECP1 = .Range("A" & Rows.Count).End(xlUp).Row
        Set rngRECP1 = .Range(.Cells(1, "A"), .Cells(lrRECP1, "I"))
        lrComp = .Range("L" & Rows.Count).End(xlUp).Row
        Set rngComp = .Range(.Cells(1, "L"), .Cells(lrComp, "T"))
    End With
    
    Ary = rngRECP1.Value2
    With rngComp
        j = lrComp
        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 Shift:=xlDown
                For zCol = 1 To Columns("I").Column
                    .Cells(j + 1, zCol).Value = Ary(i, zCol)
                Next zCol
                .Rows(j + 1).Interior.Color = vbYellow
            End If
        Next i
    End With
End Sub
 
Upvote 0
thanks mate, I might put it in a tab might be easier to sort out. I will try that code.
 
Upvote 0

Forum statistics

Threads
1,215,372
Messages
6,124,535
Members
449,169
Latest member
mm424

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