Compare two sheets with rows inserted and data upated

michellems

New Member
Joined
Apr 19, 2016
Messages
5
Hi Everyone!

I've been working on this problem for a while and I still can't figure it out. Essentially I will be taking a snapshot of data, and then comparing it to new data months later to find out what changed. I need a <acronym title="visual basic for applications" style="border-width: 0px 0px 1px; border-bottom-style: dotted; border-bottom-color: rgb(0, 0, 0); cursor: help; color: rgb(51, 51, 51); background-color: rgb(250, 250, 250);">VBA</acronym> code to highlight these differences in:
1) data in the existing item changes 2) data newly added

Sample raw data in worksheet Original:
Excel 2010
ABCDEF
1NumberAgeGenderHeight (in)Weight (lb)Comments
200121M72182good in sports
300223F69120weak in maths
400335M70175like doing sports
500431F65140playful
600527F66147hardworking girl
7
8

<colgroup><col><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Original

Sample raw data in worksheet New:


ABCDEFG
1NumberAgeGender
Weight (lb)

<tbody>
</tbody>
200321M72181
300424F80120
400535F70180
500629M75186
600731F65140
7
8
9

<colgroup><col><col><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
New
And, after the macro:
Excel 2010
ABCDEFG
1NumberAgeGender

Weight (lb)

<tbody>

</tbody>

<colgroup><col><col><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>

</tbody>
</td>[TD]
Comments​
[/TD]
[TD="align: right"][/TD]
</tr>[TR]
[TD="align: center"]2[/TD]
[TD]001[/TD]
[TD="align: right"]21[/TD]
[TD]M[/TD]
[TD="align: right"]72[/TD]
[TD="align: right"]182[/TD]
[TD="align: right"]good in sports[/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]3[/TD]
[TD]002[/TD]
[TD="align: right"]23[/TD]
[TD]F[/TD]
[TD="align: right"]69[/TD]
[TD="align: right"]120[/TD]
[TD="align: right"]weak in maths[/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]4[/TD]
[TD]003[/TD]
[TD="align: right"]21[/TD]
[TD]M[/TD]
[TD="align: right"]72[/TD]
[TD="align: right"]181[/TD]
[TD="align: right"]like doing sports[/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]5[/TD]
[TD]004[/TD]
[TD="align: right"]24[/TD]
[TD]F[/TD]
[TD="align: right"]80[/TD]
[TD="align: right"]120[/TD]
[TD="align: right"]playful[/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]6[/TD]
[TD]005[/TD]
[TD="align: right"]31[/TD]
[TD]F[/TD]
[TD="align: right"]70[/TD]
[TD="align: right"]180[/TD]
[TD="align: right"]hardworking girl[/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]7[/TD]
[TD]006[/TD]
[TD="align: right"]29[/TD]
[TD]M[/TD]
[TD="align: right"]75[/TD]
[TD="align: right"]186[/TD]
[TD="align: right"]
[/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]8[/TD]
[TD]007[/TD]
[TD="align: right"]31[/TD]
[TD]F[/TD]
[TD="align: right"]65[/TD]
[TD="align: right"]140[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[/TR]
[TR]
[TD="align: center"]9[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[/TR]
</tbody>

New
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Try the following on a COPY of your data (as VBA changes generally cannot be undone). It assumes that you data begins in Row 2 on both sheets.


Code:
Sub compare_and_highlight_different_data()
Dim vCompare, vMatch, vCompareItemData
Dim i As Long, j As Integer, LRNew As Long, LROrignal As Long

With Sheets("New")
    LRNew = .Range("A" & .Rows.Count).End(xlUp).Row
    LROriginal = Sheets("Original").Range("A" & .Rows.Count).End(xlUp).Row
    vCompare = .Range("A2:E" & LRNew)
    
    For i = 1 To UBound(vCompare)
        If Not IsError(Application.Match(vCompare(i, 1), Sheets("Original").Range("A2:A" & LROriginal), 0)) Then
            vMatch = Application.Match(vCompare(i, 1), Sheets("Original").Range("A2:A" & LROriginal), 0)
            For j = 1 To 5
                If Sheets("Original").Cells(vMatch + 1, j) <> vCompare(i, j) Then
                    .Cells(i + 1, j).Font.Color = vbRed
                End If
            Next j
        Else
            .Cells(i + 1, 1).Resize(, 5).Font.Color = vbRed
        End If
    Next i
End With
End Sub
 
Upvote 0
Dear Teeroy,

Thank you very much for your code!! It runs smoothly
It works in showing the difference between "New" and "Original" sheet and highlighted in red!

But the row with number "001" and "002" are missing, are there any commands that I can also move the old records "001 and 002" to the "New" Sheet?

Thanks a lot ind
 
Upvote 0
Dear Teeroy,

Thank you very much for your code!! It runs smoothly
It works in showing the difference between "New" and "Original" sheet and highlighted in red!

But the row with number "001" and "002" are missing, are there any commands that I can also move the old records "001 and 002" to the "New" Sheet?

Thanks a lot ind

Please try to make sure that you've listed all your requirements up front. Sometimes adding a new requirement makes it necessary to completely re-write the code, not just make a minor change.

Try the following, again on a copy...


Code:
Sub compare_and_highlight_different_data2()
Dim vCompare, vMatch, vCompareItemData, vPrimaryKey
Dim i As Long, j As Integer, LRNew As Long, LROrignal As Long
Dim objNewData As Object
Dim rMatch As Range

Set objNewData = CreateObject("Scripting.Dictionary")

With Sheets("New")
    LRNew = .Range("A" & .Rows.Count).End(xlUp).Row
    LROriginal = Sheets("Original").Range("A" & .Rows.Count).End(xlUp).Row
    vCompare = .Range("A2:E" & LRNew)
    'reset all font colors
    .Cells.Font.Color = vbBlack
    
    For i = 1 To UBound(vCompare)
        'make list of new data in a Dictionary
        objNewData.Add vCompare(i, 1), i + 1
        If Not IsError(Application.Match(vCompare(i, 1), Sheets("Original").Range("A2:A" & LROriginal), 0)) Then
            ' if matching primary key is found in original
            ' check all  data in  the row
            vMatch = Application.Match(vCompare(i, 1), Sheets("Original").Range("A2:A" & LROriginal), 0)
            For j = 1 To 5
                If Sheets("Original").Cells(vMatch + 1, j).Value <> vCompare(i, j) Then
                    .Cells(i + 1, j).Font.Color = vbRed
                End If
            Next j
        Else
            ' if no matching data is in the orignal highlight the new row
            .Cells(i + 1, 1).Resize(, 5).Font.Color = vbRed
        End If
    Next i
End With
'check original data for data not in "New" sheet and copy accross
With Sheets("Original")
    vCompare = .Range("A2:A" & LROriginal)
    For Each vPrimaryKey In vCompare
       If Not objNewData.exists(vPrimaryKey) Then
            LRNew = LRNew + 1
            ' handle the formatting of the string number being coerced into a number
            Sheets("New").Cells(LRNew, 1).NumberFormat = "@"
            vMatch = Application.Match(vPrimaryKey, .Range("A2:A" & LROriginal), 0) + 1
            Set rMatch = .Range("A" & vMatch).Resize(1, 5)
            Sheets("New").Cells(LRNew, 1).Resize(1, 5).Value = rMatch.Value
        End If
    Next
End With
End Sub
 
Upvote 0
Dear Teeroy,

Sorry to make you confused, Thanks very much for the codes! It works perfectly!
Many thanks!!!

michelle
 
Upvote 0

Forum statistics

Threads
1,215,003
Messages
6,122,655
Members
449,091
Latest member
peppernaut

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top