We help IT Professionals succeed at work.

VBA - Comparison between 2 sheets

Hi
I’m trying to do a comparison between 2 sheets Before and After on column C,G,M.
If on a row, I have a combination of the 3 columns from sheet Before, not showing in the sheets After, it would transfer the 3 columns values into sheet: Recap in column A, B and C.
The thing to remember is that each combinations will not automatically be on the same row of the other sheet, so the macro needs to look at all combinations and see it it exist in the other sheets. If it doesn’t, then it transfer the 3 values in the sheet Recap.
How can I do that?

Thanks for your help
Ex:
Sheet Before:
Before
Sheet: After
After
Sheet Recap
Recapmacro-file.xlsm
Comment
Watch Question

Top Expert 2015

Commented:
Just to rephrase if i understand correctly you want to look from sheet after to sheets before as the combination will be on the same row and if you don't find that combination..then you want to move that value in sheet recap...
Almost.

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.
Top Expert 2015

Commented:
But in your example worksheet you showing the data from after sheet..so if i understand correctly...you want to move data from before sheet to recap sheet on the combination which you don't find in after worksheet?
you are correct, sorry, i've put the wrong Recap snap shot. But still i would need that option also.
Top Expert 2015

Commented:
Okay so if i give you a option by you can choose like if you choose yes macro will move data from before to recap and if you choose no macro will move data from after to recap..will that do??
this looks like a very good option. Yes, that will do. Thanks
Top Expert 2015

Commented:
Their you go use this code..this will do what you are looking for...

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((" & ws1.Name & "!" & r.Address & "=""" & cell.Value & """)*(" & ws1.Name & "!" & r1.Address & "=" & cell.Offset(0, 4).Value & ")*(" & 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

End Sub

Open in new window


Your workbook...

Saurabh...
macro-file-1.xlsm
I did some test, but i'm facing a problem depending on what type of data i have in the cells

Can you please look at this attachment?
macro-file-1.xlsm
Top Expert 2015
Commented:
Their you go use this...

Saurabh...
macro-file-1-1.xlsm
This is very good. Exactly what i needed. Thanks a lot for your help.
Luis DiazIT consultant

Commented:
@Saurabh Singh Teotia: Your solution is great!
However I was wondering if there is a way to dynamically find the mismatch between before and after sheet.

I was thinking about a parameter table in which you specify:
some information:

Capture.GIF
Based on this information the macro should automatically read parameter sheet and ouput in recap all the mismatch values.

That means if I add a field in the comparison it should be take into account in the recap.

Additionnally is there a way to have in the recap the information of both sheets After and Before in order to see the difference?

Thank you very much for your help.
Top Expert 2015

Commented:
Hey LD16,

I'm still not clear at the end of the day what you are looking for and trying to solve..Can you open a question for the same and point me to that ..Will be more then happy to work with you to solve the same.

Saurabh...