A - Comparison between 2 sheets no 2

Hi

first topic was: VBA - Comparison between 2 sheets

I want to look from sheet after to sheets before and see if any of the combination from Sheet Before don't show in Sheet After. If there is same combinations in both sheets, it may not be on the same row from both sheets, and you may also have duplicate rows with the same combinations.

If you found combinations from Sheet Before that is not in Sheet After, the combination for the sheet before should show in sheet RECAP.

But both sheet Before and After needs to stay the way they are. I don't want to remove data from both sheets.

But right now, i'm facing a a problem where in both sheets, i have the same 3 values on both sheets but when i run the macro, it transfer the data in the RECAP sheet when it should only transfer the ones not showing in the other sheet.

If you look in the attachment, in sheer Before row 4, i have the exact same combination in sheet After row 5.

When i click Find mismatch in sheet Recap, it give me the the record showing in both sheets.

The value showing in both sheets is:
40 - 03 - CARL_JEAN


How can i update the code to fix this problem.

Thank you for your help.

    Dim str As String
    Dim r As Range, r1 As Range, r2 As Range
    Dim rng As Range, cell As Range
    Dim ws As Worksheet, ws1 As Worksheet, ws2 As Worksheet
    Dim lr As Long, lr1 As Long
    Dim mr As Long, ch As Long

    str = MsgBox("Yes will Copy Data from Before and No from After to recap", vbYesNo, "Please Choose")
    If str = vbYes Then
        Set ws = Sheets("Before")
        Set ws1 = Sheets("After")
    Else
        Set ws = Sheets("After")
        Set ws1 = Sheets("Before")

    End If

    lr = ws.Cells.Find(what:="*", searchorder:=xlRows, searchdirection:=xlPrevious).Row
    lr1 = ws1.Cells.Find(what:="*", searchorder:=xlRows, searchdirection:=xlPrevious).Row

    Set r = ws1.Range("C2:C" & lr1)
    Set r1 = ws1.Range("G2:G" & lr1)
    Set r2 = ws1.Range("M2:M" & lr1)

    Set rng = ws.Range("C2:C" & lr)

    Set ws2 = Sheets("Recap")
    mr = ws2.Cells.Find(what:="*", searchorder:=xlRows, searchdirection:=xlPrevious).Row

    If mr > 1 Then ws2.Range("A2:C" & mr).ClearContents

    For Each cell In rng

        ch = Application.Evaluate("=SumProduct((text(" & ws1.Name & "!" & r.Address & ",""@"")=""" & cell.Value & """)*(text(" & ws1.Name & "!" & r1.Address & ",""@"")=""" & cell.Offset(0, 4).Value & """)*(text(" & ws1.Name & "!" & r2.Address & ",""@"")=""" & cell.Offset(0, 10).Value & """))")
        If ch = 0 Then
            mr = ws2.Cells.Find(what:="*", searchorder:=xlRows, searchdirection:=xlPrevious).Row + 1
            ws.Range("C" & cell.Row & ",G" & cell.Row & ",M" & cell.Row).copy ws2.Range("A" & mr)
        End If
    Next cell
    
    ws2.Cells.EntireColumn.AutoFit

Open in new window

macro-file-1-1.xlsm
LVL 11
Wilder1626Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Saurabh Singh TeotiaCommented:
Use this code...

Private Sub CommandButton1_Click()
    Dim str As String
    Dim r As Range, r1 As Range, r2 As Range
    Dim rng As Range, cell As Range
    Dim ws As Worksheet, ws1 As Worksheet, ws2 As Worksheet
    Dim lr As Long, lr1 As Long
    Dim mr As Long, ch As Long

    str = MsgBox("Yes will Copy Data from Before and No from After to recap", vbYesNo, "Please Choose")
    If str = vbYes Then
        Set ws = Sheets("Before")
        Set ws1 = Sheets("After")
    Else
        Set ws = Sheets("After")
        Set ws1 = Sheets("Before")

    End If

    lr = ws.Cells.Find(what:="*", searchorder:=xlRows, searchdirection:=xlPrevious).Row
    lr1 = ws1.Cells.Find(what:="*", searchorder:=xlRows, searchdirection:=xlPrevious).Row

    Set r = ws1.Range("C2:C" & lr1)
    Set r1 = ws1.Range("G2:G" & lr1)
    Set r2 = ws1.Range("M2:M" & lr1)

    Set rng = ws.Range("C2:C" & lr)

    Set ws2 = Sheets("Recap")
    mr = ws2.Cells.Find(what:="*", searchorder:=xlRows, searchdirection:=xlPrevious).Row

    If mr > 1 Then ws2.Range("A2:C" & mr).ClearContents

    For Each cell In rng

        ch = Application.Evaluate("=SumProduct((text(" & ws1.Name & "!" & r.Address & ",""@"")=text(""" & cell.Value & """,""@""))*(text(" & ws1.Name & "!" & r1.Address & ",""@"")=text(""" & cell.Offset(0, 4).Value & """,""@""))*(text(" & ws1.Name & "!" & r2.Address & ",""@"")=text(""" & cell.Offset(0, 10).Value & """,""@"")))")
        If ch = 0 Then
            mr = ws2.Cells.Find(what:="*", searchorder:=xlRows, searchdirection:=xlPrevious).Row + 1
            ws.Range("C" & cell.Row & ",G" & cell.Row & ",M" & cell.Row).copy ws2.Range("A" & mr)
        End If
    Next cell
    
    ws2.Cells.EntireColumn.AutoFit

End Sub

Open in new window


Your workbook attached...

Saurabh...
macro-file-1-1.xlsm
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Wilder1626Author Commented:
This is exactly what i was looking for. Thanks again for your help
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.