Option Explicit
Sub LamThem()
Dim i&, j&, Lr&, T&, k&, N&
Dim Sh As Worksheet, Ws As Worksheet
Dim Arr(), Res()
Dim Endate, Ten As String, Com As String
Set Ws = Sheets("GiayBao"): Ten = Trim(Ws.[C8]): T = Trim(Ws.[D5]): N = Trim(Ws.[F5])
For Each Sh In Worksheets
If Sh.Name <> "Bangluong" Or Sh.Name <> "GiayBao" Then
If Sh.[Q3] = T And Sh.[U3] = N Then
Endate = Day(WorksheetFunction.EDate(Sh.[C5], 1) - 1)
Lr = Sh.Cells(100000, 1).End(3).Row
Arr = Sh.Range("A5:AG" & Lr).Value
ReDim Res(1 To Endate, 1 To 7)
On Error Resume Next
For i = 3 To UBound(Arr)
If Trim(Arr(i, 2)) Like Ten Then
For j = 3 To Endate + 2
If Len(Arr(i, j)) > 0 Then
k = k + 1
Res(k, 1) = k: Res(k, 2) = Arr(1, j)
Com = Trim(Sh.Cells(i + 4, j).Comment.Text)
If Len(Com) > 0 Then Res(k, 3) = Split(Com, Chr(10))(1)
If Arr(2, j) = 7 Or Arr(2, j) = "CN" Then
Res(k, 4) = 8
Res(k, 6) = Arr(i, j)
If Res(k, 6) < 1 Then
Res(k, 5) = Res(k, 4) & Ws.[H1] & Res(k, 6) * 60
Else
Res(k, 5) = Res(k, 4) + Res(k, 6)
End If
Else
Res(k, 4) = 17
Res(k, 7) = Arr(i, j)
If Res(k, 7) < 1 Then
Res(k, 5) = Res(k, 4) & Ws.[H1] & Res(k, 7) * 60
Else
Res(k, 5) = Res(k, 4) + Res(k, 7)
End If
End If
End If
Next j
Exit For
End If
Next i
End If
End If
Next Sh
If k Then
Ws.Range("A14:H53").AutoFilter
Ws.Range("A15").Resize(31, 7).ClearContents
Ws.Range("A15").Resize(k + 1, 7) = Res
Ws.Range("$A$14:$H$53").AutoFilter Field:=1, Criteria1:="<>"
End If
MsgBox " Done"
End Sub