Sub Main()
Call Update
Call LocTienDo
End Sub
Sub Update()
Dim aCD(), aData(), dic As Object
Dim sRow&, i&, ik&, iKey$
With Sheets("DATA CD")
aCD = .Range("C2", .Range("H" & .Range("C65500").End(xlUp).Row)).Value
End With
With Sheets("DATA")
aData = .Range("B3", .Range("F" & .Range("B65500").End(xlUp).Row)).Value
End With
Set dic = CreateObject("Scripting.Dictionary")
dic.comparemode = vbTextCompare
sRow = UBound(aCD)
For i = 1 To sRow
iKey = aCD(i, 1)
If dic.exists(iKey) = False Then
dic.Add iKey, Array(i, aCD(i, 6))
ElseIf aCD(i, 6) > dic.Item(iKey)(1) Then
dic.Item(iKey) = Array(i, aCD(i, 6))
End If
Next
sRow = UBound(aData)
For i = 1 To sRow
iKey = aData(i, 1)
If dic.exists(iKey) Then
If dic.Item(iKey)(1) > aData(i, 5) Then
ik = dic.Item(iKey)(0)
aData(i, 2) = aCD(ik, 2)
aData(i, 3) = aCD(ik, 3)
aData(i, 4) = aCD(ik, 4)
aData(i, 5) = aCD(ik, 6)
End If
End If
Next
Sheets("DATA").Range("B3").Resize(sRow, 5) = aData
End Sub
Sub LocTienDo()
Dim aCD(), aData(), resCD(), dic As Object
Dim sRow&, sCol&, i&, r&, r2&, k&
Set dic = CreateObject("Scripting.Dictionary")
dic.comparemode = vbTextCompare
With Sheets("DATA CD")
aCD = .Range("C2", .Range("I" & .Range("C65500").End(xlUp).Row)).Value
End With
sRow = UBound(aCD): sCol = UBound(aCD, 2)
ReDim res(1 To sRow, 1 To sCol)
For i = 1 To sRow
iKey = aCD(i, 1)
If aCD(i, 4) = 5 Then dic.Item(aCD(i, 1)) = ""
Next i
For i = 1 To sRow
iKey = aCD(i, 1)
If dic.exists(aCD(i, 1)) Then
r = r + 1
For j = 1 To sCol
res(r, j) = aCD(i, j)
Next j
Else
r2 = r2 + 1
For j = 1 To sCol
aCD(r2, j) = aCD(i, j)
Next j
End If
Next i
If r > 0 Then
erow = Sheets("DATA CD").Range("C65500").End(xlUp).Row
Sheets("DATA CD").Range("C2:I" & erow).ClearContents
Sheets("DATA CD").Range("C2").Resize(r2, sCol) = aCD
erow = Sheets("TTTT").Range("C65500").End(xlUp).Row
If erow > 1 Then Sheets("TTTT").Range("C2:I" & erow).ClearContents
Sheets("TTTT").Range("C2").Resize(r, sCol) = res
With Sheets("DATA")
aData = .Range("A3", .Range("F" & .Range("B65500").End(xlUp).Row)).Value
End With
sRow = UBound(aData): sCol = UBound(aData, 2)
For i = 1 To sRow
If dic.exists(aData(i, 2)) = False Then
k = k + 1
aData(k, 1) = k
For j = 2 To sCol
aData(k, j) = aData(i, j)
Next j
End If
Next i
erow = Sheets("DATA").Range("B65500").End(xlUp).Row
Sheets("DATA").Range("A3:F" & erow).ClearContents
Sheets("DATA").Range("A3").Resize(k, 6) = aData
End If
End Sub