Hi
I need to compare 2 sheets using Sheet2 as the base in the workbook and highlight all the differences in both sheet. I have the following macro from
the search I found here, how do I change it to compare all the cells in the columns which relates with the key word in column A.
I have attached a sample with the result highlight in red.
Sub FindDifferences()
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim ws3 As Worksheet
Dim r1 As Long
Dim m1 As Long
Dim r2 As Long
Dim m2 As Long
Dim r3 As Long
Dim oCell As Range
‘ Initialize
Set ws1 = Worksheets(“Sheet1”)
m1 = ws1.Range(“A65536”).End(xlUp).Row
Set ws2 = Worksheets(“Sheet2”)
m2 = ws2.Range(“A65536”).End(xlUp).Row
Set ws3 = Worksheets(“Differences”)
r3 = 1
‘ Clear previous results
ws3.Range(“2:65536”).ClearContents
‘ Loop through the rows of month 1
For r1 = 2 To m1
‘ Can we find a matching number in month 2?
Set oCell = ws2.Range(“A:A”).Find(What:=ws1.Range(“A” & r1), _
LookIn:=xlValues, LookAt:=xlWhole)
If oCell Is Nothing Then
‘ If not, add a new row
r3 = r3 + 1
‘ Copy columns A through D
ws1.Range(“A” & r1 & “:T” & r1).Copy Destination:=ws3.Range(“A” & r3)
‘ The difference is minus the amount from month 1
ws3.Range(“F” & r3) = -ws1.Range(“E” & r1)
ElseIf Not ws1.Range(“E” & r1) = oCell.Offset(0, 4) Then
‘ If there is a match, but the amounts are different, add a new row
r3 = r3 + 1
‘ Copy columns A through D
ws1.Range(“A” & r1 & “:T” & r1).Copy Destination:=ws3.Range(“A” & r3)
‘ Also copy BC from month 2
oCell.Offset(0, 3).Copy Destination:=ws3.Range(“E” & r3)
‘ Compute the difference
ws3.Range(“F” & r3) = oCell.Offset(0, 4) – ws1.Range(“E” & r1)
End If
Next r1
‘ Loop through the rows of month 2
For r2 = 2 To m2
‘ Can we find a matching number in month 1?
Set oCell = ws1.Range(“A:A”).Find(What:=ws2.Range(“A” & r2), _
LookIn:=xlValues, LookAt:=xlWhole)
If oCell Is Nothing Then
‘ If not, add a new row
r3 = r3 + 1
‘ Copy columns A through C and E
ws2.Range(“A” & r2 & “:C” & r2).Copy Destination:=ws3.Range(“A” & r3)
ws2.Range(“E” & r2).Copy Destination:=ws3.Range(“F” & r3)
‘ The difference is the amount from month 2
ws3.Range(“F” & r3) = ws2.Range(“E” & r2)
‘ There is no Else part, for if there is a match, it has already
‘ been handled in the first loop
End If
Next r2
End Sub
Thanks
regards, francis