• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 54
  • Last Modified:

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
0
Wilder1626
Asked:
Wilder1626
1 Solution
 
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
 
Wilder1626Author Commented:
This is exactly what i was looking for. Thanks again for your help
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now