Hi guys,
I have this code to compare two worksheets and copy the rows that are missing in the activesheet from the next sheet.
Sub Old_2_New()
' Assumes:
' -(new) sheet is selected and "Old" sheet is to the right of the (new) sheet
' - data rows starts on row 3 on both sheets
' - no dummy (non-data) rows below the last data row
Dim DestinationWS, SourceWS As Worksheet
Dim bRowExists As Boolean
Dim iInsertedRows As Integer
Set DestinationWS = ActiveSheet
Set SourceWS = ActiveSheet.Next
Application.ScreenUpdating = False
LastRowSource = SourceWS.Cells.Find(what:="*", After:=[A1], searchorder:=xlByRows, searchdirection:=xlPrevious).Row
LastRowDestination = DestinationWS.Cells.Find(what:="*", After:=[A1], searchorder:=xlByRows, searchdirection:=xlPrevious).Row
iInsertedRows = 0
For iRowIndexSource = 3 To LastRowSource
bRowExists = False
For iRowIndexDestination = 3 To LastRowDestination
If DestinationWS.Cells(iRowIndexDestination, 1) = SourceWS.Cells(iRowIndexSource, 1) And _
DestinationWS.Cells(iRowIndexDestination, 2) = SourceWS.Cells(iRowIndexSource, 2) And _
DestinationWS.Cells(iRowIndexDestination, 3) = SourceWS.Cells(iRowIndexSource, 3) And _
DestinationWS.Cells(iRowIndexDestination, 4) = SourceWS.Cells(iRowIndexSource, 4) And _
DestinationWS.Cells(iRowIndexDestination, 5) = SourceWS.Cells(iRowIndexSource, 5) Then
bRowExists = True
Exit For
End If
Next iRowIndexDestination
If Not bRowExists Then 'We found a row in the Source WS that dosn't exist in the destination sheet!
DestinationWS.Rows(LastRowDestination + 1).Insert (xlShiftDown)
SourceWS.Rows(iRowIndexSource).Copy
DestinationWS.Paste (DestinationWS.Rows(LastRowDestination + 5))
iInsertedRows = iInsertedRows + 1
End If
Next iRowIndexSource
Application.ScreenUpdating = True
MsgBox iInsertedRows & " unique rows were copied to this sheet"
End Sub
This code works great BUT here is the tricky thing though that I need your help with.
if there is a match, I want to copy the values in columns 10 to 14, 16, 19 to 23, 25, 28 to 32 and 34). So the values in the source worksheet ends up in the right column in the destination worksheet.
i'm attaching a workbook that shows what I mean. All help/ideas is appreciated.
Looking forward to your help with this issue!
Bookmarks