DRastislav
DRastislav

Reputation: 1882

Comparing two sheets - difference write to sheet 3

here is the thing: i have 2 Sheets (approx 10k rows in both) - i would like to compare them - if there is match (based on column B - do nothing, if there isn´t - value is in sheet2 but not in sheet 1 - write all columns(A,B,C,D) to sheet3) - Problem is, that first row in sheet 1 is equal maybe with 3rd row in sheet2.

How can i achieve that?

Is it possible to check it row by row according to values in column B?

Thanks

Upvotes: 0

Views: 8491

Answers (1)

Ahmad
Ahmad

Reputation: 12737

I have created a workbook with the following criteria:

Sheet1:

Column A  | Column B | Column C |  Column D | Column E
------------------------------------------------------
111024       961207    value1       data a     fake 11
111027       961209    value2       data b     fake 22
111030       961211    value3       data a     fake 33
...
...
...

and sheet 2 is a copy of sheet 1 but with a couple of rows missing.

Then I opened Visual Basic Editor (Alt+F11) and added a module, then wrote the following macro:

Sub compare()
    Sheets(3).Activate  'Go to sheet 3
    Cells.Clear         'and clear all previous results

    Range("a1").Select  'set cursor at the top

    Sheets(1).Activate  'go to sheet 1
    Range("a1").Select  'begin at the top



    Dim search_for As String   'temp variable to hold what we need to look for
    Dim cnt As Integer         'optional counter to find out how many rows we found

    Do While ActiveCell.Value <> ""   'repeat the follwoing loop until it reaches a blank row

        search_for = ActiveCell.Offset(0, 1).Value   'get a hold of the value in column B

        Sheets(2).Activate  'go to sheet(2)

        On Error Resume Next   'incase what we search for is not found, no errors will stop the macro

        Range("b:b").Find(search_for).Select  'find the value in column B of sheet 2

        If Err <> 0 Then   'If the value was not found, Err will not be zero

            On Error GoTo 0  'clearing the error code

            Sheets(1).Activate   'go back to sheet 1

            r = ActiveCell.Row   'get a hold of current row index

            Range(r & ":" & r).Select  'select the whole row

            cnt = cnt + 1   'increment the counter

            Selection.Copy  'copy current selection

            Sheets(3).Activate  'go to sheet 3

            ActiveCell.PasteSpecial xlPasteAll  'Past the entire row to sheet 3

            ActiveCell.Offset(1, 0).Select  'go down one row to prepare for next row.


        End If
        Sheets(1).Activate   'return to sheet 1
        ActiveCell.Offset(1, 0).Select   'go to the next row

    Loop   'repeat

    Sheets(3).Activate    'go to sheet 3 to examine findings

    MsgBox "I have found " & cnt & " rows that did not exist in sheet 2"

End Sub

Then I ran the macro and found it is working..

I hope this answer helped you achieve what you want.

If you want, here is the Excel workbook I created You will need to Enable macro before you could see the code run. Office will automatically warn you of any Excel file that contain macros.

Upvotes: 2

Related Questions