Sub Taodulieu()
Dim Rdata As Long, i As Long, Rw As Long, Rlk As Long, DemHD As Byte
Dim SoCT As Range, Data As Range, Ws As Object
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Rdata = [A65536].End(xlUp).Row
Set SoCT = Range("A4:A" & Rdata)
Set Sotien = SoCT.Offset(, 8)
Set Data = SoCT.Resize(, 9)
Set Ws = WorksheetFunction
Range("K4:S65536").ClearContents
Range("A3:A" & Rdata).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=[K3], Unique:=True
Rw = [K65536].End(xlUp).Row
For i = 4 To Rw
With Cells(i, 11)
Rlk = Ws.Match(.Value, SoCT, 0) + 3
.Offset(, 1) = Cells(Rlk, 3)
.Offset(, 2) = Cells(Rlk, 4)
.Offset(, 3) = Cells(Rlk, 5)
.Offset(, 4) = Cells(Rlk, 6)
'Tim TK No
If Cells(Rlk, 7) = Cells(Rlk + 1, 7) Then
.Offset(, 5) = Cells(Rlk, 7)
Else: .Offset(, 5) = Cells(Rlk, 7) & ", " & Cells(Rlk + 1, 7)
End If
'Tim TK Co
If Cells(Rlk, 8) = Cells(Rlk + 1, 8) Then
.Offset(, 6) = Cells(Rlk, 8)
Else: .Offset(, 6) = Cells(Rlk, 8) & ", " & Cells(Rlk + 1, 8)
End If
'Tim SoHD : VD o day la 1 chung tu co 3 hoa don
DemHD = Ws.CountIf(SoCT, .Value) / 2
Select Case DemHD
Case 1: .Offset(, 8) = Cells(Rlk, 2)
Case 2: .Offset(, 8) = Cells(Rlk, 2) & ", " & Cells(Rlk + 2, 2)
Case 3: .Offset(, 8) = Cells(Rlk, 2) & ", " & Cells(Rlk + 2, 2) & ", " & Cells(Rlk + 4, 2)
'.......................
'Case n voi n so hoa don
End Select
'Tim So tien
.Offset(, 7) = Ws.SumIf(SoCT, .Value, Sotien)
End With
Next
Set SoCT = Nothing: Set Sotien = Nothing: Set Data = Nothing: Set Ws = Nothing
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub