Private Sub Worksheet_Change(ByVal Target As Range)
Dim sCell As Range, kt As Boolean, MyRang As Range
If Target.Cells.Count > 1 Then Exit Sub
If Not Intersect(Target, Union([a3:a1000], [c3:c1000])) Is Nothing Then
kt = False
Cells.Interior.Pattern = xlNone
If Not Intersect(Target, [a3:a1000]) Is Nothing Then
For Each sCell In Range("A3:A" & Range("A65000").End(xlUp).Row)
If UCase(Replace(sCell.Value, " ", "")) = UCase(Replace(Target.Value, " ", "")) Then
If MyRang Is Nothing Then
Set MyRang = sCell
Else
Set MyRang = Union(MyRang, sCell)
End If
If (sCell.Address <> Target.Address) And (sCell.Offset(, 2).Value = Target.Offset(, 2).Value) Then
sCell.Interior.Color = 255
kt = True
End If
End If
Next
Else
For Each sCell In Range("C3:C" & Range("C65000").End(xlUp).Row)
If UCase(Replace(sCell.Value, " ", "")) = UCase(Replace(Target.Value, " ", "")) Then
If MyRang Is Nothing Then
Set MyRang = sCell
Else
Set MyRang = Union(MyRang, sCell)
End If
sCell.Interior.Color = 255
If (sCell.Address <> Target.Address) And (sCell.Offset(, -2).Value = Target.Offset(, -2).Value) Then
sCell.Interior.Color = 255
kt = True
End If
End If
Next
End If
If kt Then
MsgBox "Du lieu da bi trung, de nghi ban nhap lai"
Cells.Interior.Pattern = xlNone
Application.EnableEvents = False
Target.Value = Empty
Target.Select
Application.EnableEvents = True
ElseIf Not MyRang Is Nothing Then
MyRang.Interior.Pattern = xlNone
End If
End If
End Sub