VBA - Before After macro

Hi

I have a Excel file with 3 sheets:
Before
After
Recap

I need to validate the differences between the Before and After sheet and put transfer the results in sheet called  Recap.

In attachment, if you look at sheet Before in the Test4 column, you will see some values. I need to search for the values not showing in sheet After in the same column and transfer the values missing in the sheet Recap column A starting at Row 2 going down.

I need to also do the same thing with Sheet After column Test 4 not showing in Sheet Before column Tes4. Result of missing value will go into Recap column B.

How can i do that?

In attachment you will have a sample and the final result i should get in the Recap sheet.
Thanks
before-after.xlsm
LVL 11
Wilder1626Asked:
Who is Participating?
 
Saurabh Singh TeotiaCommented:
Their you go run this code...

Private Sub CommandButton1_Click()
    Dim ws As Worksheet, ws1 As Worksheet, ws2 As Worksheet
    Dim rng As Range, cell As Range, r As Range
    Dim lrow As Long, lr As Long, lr1 As Long

    Set ws = Sheets("Before")
    Set ws1 = Sheets("After")
    Set ws2 = Sheets("Recap")

    lr1 = ws2.Cells(Cells.Rows.Count, "A").End(xlUp).Row

    If lr1 > 1 Then ws2.Range("A2:A" & lr1).Clear

    lr1 = ws2.Cells(Cells.Rows.Count, "B").End(xlUp).Row

    If lr1 > 1 Then ws2.Range("B2:B" & lr1).Clear


    lrow = ws.Cells(Cells.Rows.Count, "d").End(xlUp).Row
    Set rng = ws.Range("D2:D" & lrow)

    lr = ws1.Cells(Cells.Rows.Count, "d").End(xlUp).Row
    Set r = ws1.Range("D2:D" & lr)


    For Each cell In rng

        If Application.WorksheetFunction.CountIf(r, cell.Value) = 0 Then

            lr1 = ws2.Cells(Cells.Rows.Count, "a").End(xlUp).Row + 1

            ws2.Cells(lr1, 1).Value = cell.Value

        End If

    Next cell



    For Each cell In r

        If Application.WorksheetFunction.CountIf(rng, cell.Value) = 0 Then

            lr1 = ws2.Cells(Cells.Rows.Count, "b").End(xlUp).Row + 1

            ws2.Cells(lr1, 2).Value = cell.Value

        End If

    Next cell

End Sub

Open in new window


Your workbook for your reference...

Saurabh...
before-after.xlsm
0
 
Wilder1626Author Commented:
Thanks for your help. This is what i was looking for.
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.

All Courses

From novice to tech pro — start learning today.