Try this code
Option Explicit
Sub compare_bks()
Dim lrow As Long, lrow1 As Long, i As Long
With ThisWorkbook.Worksheets(1)
.Range("H1").Value = "Book"
lrow = .Range("A" & .Rows.Count).End(xlUp).Row
.Range("H2:H" & lrow).Value = "1"
lrow1 = .Range("H" & .Rows.Count).End(xlUp).Row
End With
With Workbooks("Book2.xls").Worksheets(1)
lrow = .Range("A" & .Rows.Count).End(xlUp).Row
.Range("A2:G" & lrow).Copy ThisWorkbook.Worksheets(1).Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
End With
With ThisWorkbook.Worksheets(1)
lrow = .Range("A" & .Rows.Count).End(xlUp).Row
.Range("H" & lrow1 + 1 & ":H" & lrow).Value = "2"
End With
With ThisWorkbook.Worksheets(1)
.Sort.SortFields.Add Key:=Range("B:B") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With .Sort
.SetRange Range("A:H")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
lrow = .Range("A" & .Rows.Count).End(xlUp).Row
For i = 2 To lrow
If .Range("B" & i).Value <> .Range("B" & i + 1).Value Then
.Range("A" & i & ":H" & i).Interior.Color = 65535
Else
i = i + 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 (in this case book1.xls)
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.
Bookmarks