Sub TongHop()
Dim Path As String, FileName As String
Dim Wb As Workbook
Dim sArr(), dArr(), Res(), CapNhat() As Boolean
Dim ten As String, lop As String, lopCN As String, key As String, tmp As String
Dim i As Long, j As Byte, n As Byte
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Path = ThisWorkbook.Path
FileName = GetFile(ThisWorkbook.Path)
Set Wb = Workbooks.Open(FileName)
i = Wb.ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
If i > 10 Then sArr = Wb.ActiveSheet.Range("A1:O" & i).Value
Wb.Close False
Set Wb = Nothing
If i < 11 Then MsgBox ("File khong co du lieu"): GoTo Thoat
With Sheets("Sheet1")
dArr = .Range("C2:D" & .Range("C" & Rows.Count).End(xlUp).Row).Value
End With
ReDim Res(1 To 1, 1 To 3)
ReDim CapNhat(1 To UBound(dArr))
With CreateObject("Scripting.dictionary")
For i = 1 To UBound(dArr)
ten = UCase(Application.Trim(dArr(i, 1)))
lop = UCase(Application.Trim(dArr(i, 2)))
key = ten & "#" & lop
If Not .exists(key) Then
.Add key, i
Else
For j = 1 To 10
tmp = key & "#" & j
If Not .exists(tmp) Then .Add tmp, i: Exit For
Next j
End If
Next i
For i = 5 To UBound(sArr) Step 25
For n = 1 To 9 Step 8
ten = UCase(Application.Trim(sArr(i, n)))
lop = UCase(Application.Trim(sArr(i + 1, n)))
If Len(ten) > 11 And Len(lop) > 5 Then
key = Mid(ten, 12, Len(ten)) & "#" & Mid(lop, 6, Len(lop))
ik = 0
If .exists(key) Then
ik = .Item(key)
.Remove (key)
Else
For j = 1 To 10
tmp = key & "#" & j
If .exists(tmp) Then
ik = .Item(tmp)
.Remove (tmp)
Exit For
End If
Next j
End If
If ik Then
Res(1, 1) = sArr(i + 6, n + 6)
Res(1, 2) = sArr(i + 7, n + 6)
Res(1, 3) = sArr(i + 8, n + 6)
Range("E1:G1").Offset(ik) = Res
If lopCN = Empty Then lopCN = Mid(lop, 6, Len(lop))
CapNhat(ik) = True
End If
End If
Next n
Next i
End With
For i = 1 To UBound(dArr)
If lopCN = UCase(Application.Trim(dArr(i, 2))) And CapNhat(i) = False Then Range("E1:G1").Offset(i).ClearContents
Next i
Thoat:
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub