Option Explicit
Sub TKNgayNghi()
Dim Sh As Worksheet, Clls As Range, Rng As Range, sRng As Range
Const dF As String = "MM/dd/yyyy"
Dim MyColor As Byte, The As String, Tru As Integer
Sheets("NgayNghi").Select: Application.ScreenUpdating = False
Columns("A:H").Sort Key1:=Range("C2"), Order1:=xlAscending, Key2:=Range("A2") _
, Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1
Set Sh = Sheets("BaoCao"): MyColor = Sh.[A1].Interior.ColorIndex
Sh.[A1].Interior.ColorIndex = IIf(MyColor < 42, 1 + MyColor, 34)
Set Rng = Sh.Range(Sh.[d1], Sh.[iv1].End(xlToLeft))
Sh.[b2].CurrentRegion.Offset(2, 1).ClearContents
For Each Clls In Range([c2], [c2].End(xlDown))
With Sh.[b65500].End(xlUp).Offset(1)
If Clls.Offset(, -2).Value <> Clls.Offset(-1, -2).Value Then
.Resize(, 3).Value = Clls.Offset(, -2).Resize(, 3).Value
The = Clls.Offset(, -2).Value
Tru = 0
Else
If Tru = 0 Then Tru = 1 Else Tru = 0 '*'
End If
Set sRng = Rng.Find(Clls.Offset(, 4).Value, , xlFormulas, xlPart)
If sRng Is Nothing Then
MsgBox "Xem Lai Di Nha": Clls.Interior.ColorIndex = 38
Exit Sub
Else
Sh.Cells(.Row - Tru, sRng.Column).Value = Clls.Offset(, 3).Value
Sh.Cells(.Row - Tru, 1 + sRng.Column).Value = Format(Clls.Offset(, 1).Value, dF)
End If
If Clls.Value <> Clls.Offset(1).Value Then _
.Offset(1).Value = "SubTotal"
End With
Next Clls
Sh.Select
End Sub