Hi all
I have following code but I have a problem with the following line:
Range(.Cells(Rows.Count, "A").End(xlUp), (.Cells(Rows.Count, LstCol).End(xlUp))).Interior.ColorIndex = 3
On Error Resume Next
It highlights all the cells above red rather the one line it is supposed to.
Any ideas?
Sub HighlightTheDifferences()
Dim LstRw1 As Long, LstCol1 As Long, List1 As Range
Dim LstRw2 As Long, LstCol2 As Long, List2 As Range
Dim FirstSht As String, SecondSht As String
Dim SrchName As Range, FndName As Range, RedRng As Range
Dim LstCol As Long, i As Integer
FirstSht = Sheets(1).Name
SecondSht = Sheets(2).Name
Application.ScreenUpdating = False
With Sheets(FirstSht)
.Cells.Interior.ColorIndex = xlNone
LstRw1 = .Cells(Rows.Count, "A").End(xlUp).Row
LstCol1 = .Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
.Columns(1).Insert
With .Cells(2, "A")
.Formula = "=b2&c2"
.Copy .Range("A2:A" & LstRw1)
.Range("A1:A" & LstRw1) = .Range("A1:A" & LstRw1).Value
End With
Set List1 = .Range("A1:A" & LstRw1)
End With
With Sheets(SecondSht)
.Cells.Interior.ColorIndex = xlNone
LstRw2 = .Cells(Rows.Count, "A").End(xlUp).Row
LstCol2 = .Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
.Columns(1).Insert
With .Cells(2, "A")
.Formula = "=b2&c2"
.Copy .Range("A2:A" & LstRw2)
.Range("A1:A" & LstRw2) = .Range("A1:A" & LstRw2).Value
End With
Set List2 = .Range("A1:A" & LstRw2)
End With
If LstCol1 > LstCol2 Then LstCol = LstCol1 + 1 Else LstCol = LstCol2 + 1
For Each SrchName In List1
If Application.WorksheetFunction.CountA(List2, SrchName) > 0 Then
Set FndName = List2.Find(SrchName, LookAt:=xlContents)
If Not FndName Is Nothing Then
For i = 1 To LstCol
If Sheets(FirstSht).Cells(SrchName.Row, i).Value <> Sheets(SecondSht).Cells(FndName.Row, i).Value Then
Sheets(FirstSht).Cells(SrchName.Row, i).Interior.ColorIndex = 6
Sheets(SecondSht).Cells(FndName.Row, i).Interior.ColorIndex = 6
End If
Next i
Else
For i = 2 To LstCol
If Sheets(FirstSht).Cells(SrchName.Row, i).Value <> "" Then _
Sheets(FirstSht).Cells(SrchName.Row, i).Interior.ColorIndex = 4
Next i
End If
End If
Next SrchName
For Each SrchName In List2
With Sheets(FirstSht)
Set FndName = List1.Find(SrchName, LookAt:=xlContents)
If FndName Is Nothing Then
SrchName.EntireRow.Copy .Cells(Rows.Count, "a").End(xlUp)(6)
Range(.Cells(Rows.Count, "A").End(xlUp), (.Cells(Rows.Count, LstCol).End(xlUp))).Interior.ColorIndex = 3
On Error Resume Next
Range(.Cells(Rows.Count, "A").End(xlUp), (.Cells(Rows.Count, LstCol).End(xlUp))).SpecialCells(xlCellTypeBlanks).Interior.ColorIndex = xlNone
On Error GoTo 0
End If
End With
Next SrchName
Sheets(FirstSht).Columns(1).EntireColumn.Delete
Sheets(SecondSht).Columns(1).EntireColumn.Delete
Application.ScreenUpdating = True
End Sub
I have following code but I have a problem with the following line:
Range(.Cells(Rows.Count, "A").End(xlUp), (.Cells(Rows.Count, LstCol).End(xlUp))).Interior.ColorIndex = 3
On Error Resume Next
It highlights all the cells above red rather the one line it is supposed to.
Any ideas?
Sub HighlightTheDifferences()
Dim LstRw1 As Long, LstCol1 As Long, List1 As Range
Dim LstRw2 As Long, LstCol2 As Long, List2 As Range
Dim FirstSht As String, SecondSht As String
Dim SrchName As Range, FndName As Range, RedRng As Range
Dim LstCol As Long, i As Integer
FirstSht = Sheets(1).Name
SecondSht = Sheets(2).Name
Application.ScreenUpdating = False
With Sheets(FirstSht)
.Cells.Interior.ColorIndex = xlNone
LstRw1 = .Cells(Rows.Count, "A").End(xlUp).Row
LstCol1 = .Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
.Columns(1).Insert
With .Cells(2, "A")
.Formula = "=b2&c2"
.Copy .Range("A2:A" & LstRw1)
.Range("A1:A" & LstRw1) = .Range("A1:A" & LstRw1).Value
End With
Set List1 = .Range("A1:A" & LstRw1)
End With
With Sheets(SecondSht)
.Cells.Interior.ColorIndex = xlNone
LstRw2 = .Cells(Rows.Count, "A").End(xlUp).Row
LstCol2 = .Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
.Columns(1).Insert
With .Cells(2, "A")
.Formula = "=b2&c2"
.Copy .Range("A2:A" & LstRw2)
.Range("A1:A" & LstRw2) = .Range("A1:A" & LstRw2).Value
End With
Set List2 = .Range("A1:A" & LstRw2)
End With
If LstCol1 > LstCol2 Then LstCol = LstCol1 + 1 Else LstCol = LstCol2 + 1
For Each SrchName In List1
If Application.WorksheetFunction.CountA(List2, SrchName) > 0 Then
Set FndName = List2.Find(SrchName, LookAt:=xlContents)
If Not FndName Is Nothing Then
For i = 1 To LstCol
If Sheets(FirstSht).Cells(SrchName.Row, i).Value <> Sheets(SecondSht).Cells(FndName.Row, i).Value Then
Sheets(FirstSht).Cells(SrchName.Row, i).Interior.ColorIndex = 6
Sheets(SecondSht).Cells(FndName.Row, i).Interior.ColorIndex = 6
End If
Next i
Else
For i = 2 To LstCol
If Sheets(FirstSht).Cells(SrchName.Row, i).Value <> "" Then _
Sheets(FirstSht).Cells(SrchName.Row, i).Interior.ColorIndex = 4
Next i
End If
End If
Next SrchName
For Each SrchName In List2
With Sheets(FirstSht)
Set FndName = List1.Find(SrchName, LookAt:=xlContents)
If FndName Is Nothing Then
SrchName.EntireRow.Copy .Cells(Rows.Count, "a").End(xlUp)(6)
Range(.Cells(Rows.Count, "A").End(xlUp), (.Cells(Rows.Count, LstCol).End(xlUp))).Interior.ColorIndex = 3
On Error Resume Next
Range(.Cells(Rows.Count, "A").End(xlUp), (.Cells(Rows.Count, LstCol).End(xlUp))).SpecialCells(xlCellTypeBlanks).Interior.ColorIndex = xlNone
On Error GoTo 0
End If
End With
Next SrchName
Sheets(FirstSht).Columns(1).EntireColumn.Delete
Sheets(SecondSht).Columns(1).EntireColumn.Delete
Application.ScreenUpdating = True
End Sub