I worked out this code for you -
Option Explicit
Sub compare_sheets()
Dim lrow As Long, lrow1 As Long, i As Long
With Worksheets("Sheet2")
lrow = .Range("A" & .Rows.Count).End(xlUp).Row
.Range("H2:H" & lrow).Value = "Sheet2"
lrow = Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
Worksheets("Sheet1").Range("A1:G" & lrow).Copy
.Range("A" & .Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial (xlPasteValues)
lrow = .Range("H" & .Rows.Count).End(xlUp).Row
lrow1 = .Range("A" & .Rows.Count).End(xlUp).Row
.Range("H" & lrow + 1 & ":H" & lrow1).Value = "Sheet1"
.Sort.SortFields.Add Key:=.Range("A:A") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.Sort.SortFields.Add Key:=.Range("H:H") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With .Sort
.SetRange Range("A:H")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
For i = lrow1 To 2 Step -1
If .Range("A" & i).Value = .Range("A" & i - 1).Value And .Range("B" & i).Value = .Range("B" & i - 1).Value And _
.Range("C" & i).Value = .Range("C" & i - 1).Value And .Range("D" & i).Value = .Range("D" & i - 1).Value And _
.Range("E" & i).Value = .Range("E" & i - 1).Value And .Range("F" & i).Value = .Range("F" & i - 1).Value And _
.Range("G" & i).Value = .Range("G" & i - 1).Value Then
If .Range("H" & i - 1).Value = "Sheet1" Then
.Rows(i - 1).Delete
lrow = lrow - 1
.Range("A" & i & ":G" & i).Interior.Color = 65535
End If
ElseIf .Range("H" & i - 1).Value = "Sheet1" Then
.Rows(i - 1).Delete
lrow = lrow - 1
End If
Next i
End With
End Sub
Copy the Excel VBA code
Select the workbook in which you want to store the Excel VBA code
Hold the Alt key, and press the F11 key, to open the Visual Basic Editor
Choose Insert | Module
Where the cursor is flashing, choose Edit | Paste
To run the Excel VBA code:
Choose View | Macros
Select a macro in the list, and click the Run button
However, i didnt find any duplicates because of the Price Type field. All other fields were matching to make the rows duplicate but there was a difference in the Price Type field which was not allowing the code to consider it as a duplicate.
Bookmarks