Option Explicit
Sub vData()
Dim i, j, k, l, intL, intD, intTMP, edRow, edRowC, intKQ, l2KQ As Integer
Dim SArr, Result, PhongBan, KQ
Dim ws As Worksheet
Dim DicT As Dictionary
Application.ScreenUpdating = False
intKQ = 0
Set DicT = New Scripting.Dictionary
SArr = Sheet1.Range("B6:F" & Sheet1.Cells(Sheet1.Rows.Count, "B").End(xlUp).Row).Value
ReDim Result(1 To UBound(SArr, 1), 1 To UBound(SArr, 2))
Set ws = ActiveSheet
For intL = 1 To UBound(SArr, 1) - 1
If Month(SArr(intL, 4)) = Month(ws.Range("F2").Value) And Month(SArr(intL, 5)) = Month(ws.Range("G2").Value) Then
If Not Day(SArr(intL, 4) > Day(ws.Range("G2").Value)) Then
If Not Day(SArr(intL, 5) < Day(ws.Range("F2").Value)) Then
If Day(ws.Range("F2").Value) < Day(SArr(intL, 4)) And Day(SArr(intL, 5)) < Day(ws.Range("G2").Value) Then
intD = intD + 1
Result(intD, 1) = SArr(intL, 1)
Result(intD, 2) = SArr(intL, 2)
Result(intD, 3) = SArr(intL, 3)
Result(intD, 4) = SArr(intL, 4)
Result(intD, 5) = SArr(intL, 5)
ElseIf Day(ws.Range("F2").Value) > Day(SArr(intL, 4)) And Day(SArr(intL, 5)) > Day(ws.Range("G2").Value) Then
intD = intD + 1
Result(intD, 1) = SArr(intL, 1)
Result(intD, 2) = SArr(intL, 2)
Result(intD, 3) = SArr(intL, 3)
Result(intD, 4) = ws.Range("F2").Value
Result(intD, 5) = ws.Range("G2").Value
End If
End If
End If
End If
Next
ReDim KQ(1 To UBound(Result, 1), 1 To UBound(Result, 2))
ReDim PhongBan(1 To 4, 1 To 1)
For i = 1 To UBound(Result, 1)
If Not DicT.Exists(Result(i, 3)) Then
j = j + 1
DicT.Add Result(i, 3), j
PhongBan(j, 1) = Result(i, 3)
End If
Next
For k = 1 To UBound(PhongBan, 1)
intD = 0
For l = 1 To UBound(Result, 1)
If PhongBan(k, 1) = Result(l, 3) Then
With ActiveSheet
edRow = Cells(Rows.Count, "C").End(xlUp).Row
End With
intD = intD + 1
If Not Cells(edRow, 2).Value = PhongBan(k, 1) Then
Cells(edRow + 1, 2) = PhongBan(k, 1)
Else
End If
Cells(edRow + 1, 3) = Result(l, 1)
Cells(edRow + 1, 4) = Result(l, 2)
Cells(edRow + 1, 5) = Result(l, 3)
Cells(edRow + 1, 6) = Result(l, 4)
Cells(edRow + 1, 7) = Result(l, 5)
End If
Next
Next
Application.ScreenUpdating = True