Results 1 to 5 of 5

Comparing two worksheets and copy non matching rows

Threaded View

  1. #1
    Registered User
    Join Date
    02-23-2011
    Location
    Stockholm, Sweden
    MS-Off Ver
    Excel 2003, Excel 2010
    Posts
    33

    Comparing two worksheets and copy non matching rows

    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!
    Attached Files Attached Files
    Last edited by thylander; 07-18-2011 at 09:18 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1