Sub ThanhToan()
Dim sArr(), aMa(), Res(), iKey$, tmp$
Dim i&, iR&, sRow&
With Sheets("Du Lieu 2") 'Sheet Du lieu
i = .Range("E" & Rows.Count).End(xlUp).Row
If i < 2 Then MsgBox ("Khong co du lieu"): Exit Sub
sArr = .Range("A2:E" & i).Value
End With
With Sheets("Du Lieu 2") 'Sheet ket qua
i = .Range("G" & Rows.Count).End(xlUp).Row 'Cot "G" la cot Ma
If i < 5 Then MsgBox ("Khong co du lieu"): Exit Sub
aMa = .Range("G5:G" & i).Value 'Cot "G" la cot Ma, "5" là dong dau
End With
With CreateObject("scripting.dictionary")
sRow = UBound(aMa)
ReDim Res(1 To sRow, 1 To 2)
For i = 1 To sRow
iKey = aMa(i, 1)
If Not .exists(iKey) Then .Add iKey, i
Next i
sRow = UBound(sArr)
For i = 1 To sRow
iKey = sArr(i, 1)
If .exists(iKey) Then
iR = .Item(iKey)
If IsNumeric(sArr(i, 4)) Then Res(iR, 1) = Res(iR, 1) + sArr(i, 4)
Res(iR, 2) = Res(iR, 2) & ", " & Format(sArr(i, 5), "dd/mm/yyyy")
End If
Next i
End With
sRow = UBound(Res)
For i = 1 To sRow
tmp = Res(i, 2)
If tmp <> Empty Then
Res(i, 2) = Mid(tmp, 3, Len(tmp))
If Len(Res(i, 2)) = 10 Then Res(i, 2) = "'" & Res(i, 2)
End If
Next i
With Sheets("Du Lieu 2") 'Sheet ket qua
.Range("H5").Resize(sRow, 2).Value = Res '"H5" la o dau cua ket qua
End With
End Sub