Question

Hoping someone can help me - I have been reading through the forum for hours now and have found some codes and been tweaking them but cannot figure out how to make them work for me.

I have a file with 2 worksheets. I need to look at column B (userid),D (date), G (time) from worksheet1 and find all that do not match worksheet 2 column A (userid),B (date),C(time) and put all non matching data (All rows/columns) from worksheet1 onto worksheet3. I went the route of an array formula but the requirements want something cleaner and to only show non matching information in a separate sheet so I was thinking VBA would be the easiest.

Sample Data FILE: https://docs.google.com/file/d/0B-05LU9z79UTQUs3QnRSSUZETmM/edit

Worksheet1

Associate ID    Organization Name   Original Start Date Segment Start Date  Segment End Date    Time
83010   abc 4/8/2014    3/31/2014   4/1/2014    465
89551   abc 4/10/2014   4/1/2014    4/1/2014    30
90111   abc 4/9/2014    4/7/2014    4/7/2014    30
90136   abc 4/9/2014    4/7/2014    4/7/2014    445

Worksheet2

ED_EMP_NB   SCHED_DT    DURATION_MIN_AM
083010  4/8/2014    465
089551  4/10/2014   60
090111  4/9/2014    60
090136  4/9/2014    445

UPDATE: So I took your code tmoore82 and updated it to reference sheets(3) and the offset numbers to match the rows (I believe). Its pulling back 7 of the 14 non matching rows.. Can you help me find the errors?

Sub Test2()

Dim rowCount1 As Long
Dim rowCount2 As Long

''EDITED TO CALL ON SHEET 3
rowCount1 = ThisWorkbook.Sheets(1).Range("B20").SpecialCells(xlCellTypeLastCell).Row
rowCount2 = ThisWorkbook.Sheets(3).Range("B2").SpecialCells(xlCellTypeLastCell).Row

Dim rng1 As Range
Dim rng2 As Range

''EDITED TO CALL ON SHEET 3
Set rng1 = ThisWorkbook.Sheets(1).Range("B20:B" & rowCount1)
Set rng2 = ThisWorkbook.Sheets(3).Range("B2:B" & rowCount2)

Dim currentRow As Long
currentRow = 2

''UPDATED OFFSET TO MATCH ROWS IN SHEET 3
For Each cell In rng1.Cells
For Each cell2 In rng2.Cells
If cell2.Value <> cell.Value And cell2.Offset(0, 5).Value <> cell.Offset(0, 5).Value And cell2.Offset(0, 2).Value <> cell.Offset(0, 2).Value Then
ThisWorkbook.Sheets(1).Rows(cell.Row).Copy Destination:=ThisWorkbook.Sheets(4).Range("A" & currentRow)
currentRow = currentRow + 1
GoTo NextIteration
End If
Next cell2
NextIteration:
Next cell

End Sub
Was it helpful?

Solution

Okay, this isn't the most efficient code, but it worked on the sample data. As long as your data isn't terribly huge, this should get you started:

Sub Test()

    Dim rowCount1 As Long
    Dim rowCount2 As Long

    rowCount1 = ThisWorkbook.Sheets(1).Range("A2").SpecialCells(xlCellTypeLastCell).Row
    rowCount2 = ThisWorkbook.Sheets(2).Range("A2").SpecialCells(xlCellTypeLastCell).Row

    Dim rng1 As Range
    Dim rng2 As Range

    Set rng1 = ThisWorkbook.Sheets(1).Range("A2:A" & rowCount1)
    Set rng2 = ThisWorkbook.Sheets(2).Range("A2:A" & rowCount2)

    Dim sheet1() As Variant
    ReDim sheet1(rowCount1 - 1, 2)

    Dim n As Long
    n = 0

    For Each cell In rng1.Cells
        sheet1(n, 0) = cell.Value
        sheet1(n, 1) = cell.Offset(0, 2).Value
        sheet1(n, 2) = cell.Offset(0, 5).Value
        Debug.Print cell.Value
        n = n + 1
    Next cell

    Dim currentRow As Long
    currentRow = 1

    For n = 0 To UBound(sheet1)
        For Each cell In rng2.Cells
            If cell.Value = sheet1(n, 0) And cell.Offset(0, 1).Value = sheet1(n, 1) And cell.Offset(0, 2).Value = sheet1(n, 2) Then
                ThisWorkbook.Sheets(1).Rows(n + 2).Copy Destination:=ThisWorkbook.Sheets(3).Range("A" & currentRow)
                currentRow = currentRow + 1
                 GoTo NextIteration
            End If
         Next cell
NextIteration:
    Next n

End Sub

Response to New Code

Change the loops to the following:

''UPDATED OFFSET TO MATCH ROWS IN SHEET 3
For Each cell In rng1.Cells
For Each cell2 In rng2.Cells
    If cell2.Value = cell.Value And cell2.Offset(0, 5).Value = cell.Offset(0, 5).Value And cell2.Offset(0, 2).Value = cell.Offset(0, 2).Value Then
'ThisWorkbook.Sheets(1).Rows(cell.Row).Cop Destination:=ThisWorkbook.Sheets(4).Range("A" & currentRow)
'currentRow = currentRow + 1
        GoTo NextIteration
    End If
Next cell2
ThisWorkbook.Sheets(1).Rows(cell.Row).Copy Destination:=ThisWorkbook.Sheets(4).Range("A" & currentRow)
currentRow = currentRow + 1
NextIteration:
Next cell

Do you see why that works?

Final answer

Blogged about it here: http://htddi.wordpress.com/2014/05/16/anatomy-of-a-stackoverflow-discussion/

and here: http://htddi.wordpress.com/2014/05/16/anatomy-of-a-stackoverflow-discussion-part-ii/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top