Sub XoaTrung()
Dim sArr(), Res(), iKey As String
Dim i As Long, k As Long, ik As Long, eRow As Long, j As Byte
With Sheets("Sheet 1")
eRow = .Range("A1000000").End(xlUp).Row
If eRow < 3 Then Exit Sub
sArr = .Range("A1:E" & eRow).Value
End With
ReDim Res(1 To eRow, 1 To 5)
With CreateObject("Scripting.Dictionary")
For i = 2 To eRow
iKey = CStr(sArr(i, 1))
If Len(iKey) Then
If Not .exists(iKey) Then
If Len(sArr(i, 3)) = 0 Then .Add iKey, i Else .Add iKey, 0
Else
ik = .Item(iKey)
If Len(sArr(i, 3)) > 0 And ik > 0 Then .Item(iKey) = 0 Else ik = i
sArr(ik, 1) = "Xu Tram"
End If
End If
Next i
End With
For i = 1 To eRow
If sArr(i, 1) <> "Xu Tram" Then
k = k + 1
For j = 1 To 5
Res(k, j) = sArr(i, j)
Next j
End If
Next i
Application.ScreenUpdating = False
With Sheets("Sheet2")
eRow = .Range("A1000000").End(xlUp).Row
.Range("A1:E" & eRow).ClearContents
If k Then
.Range("A1:A" & k).NumberFormat = "@"
.Range("A1:E" & k) = Res
End If
End With
Application.ScreenUpdating = True
End Sub