Public Sub Up_BCCT()
Dim Dic As Object, eArr(), sArr(), dArr(), Tmp, I As Long, J As Long, K As Long, Tem As String
Dim fDate As Date, eDate As Date, Rws As Long, LuuKho&, LuuKhoT As Long
Dim Sh As Worksheet
Set Sh = Sheets("BCCT")
fDate = [C2].Value: eDate = [E2].Value
LuuKho = DateDiff("m", fDate, eDate)
Set Dic = CreateObject("Scripting.Dictionary")
With Sheets("DMVT")
eArr = .Range(.[b6], .[b6].End(xlDown)).Resize(, 12).Value
End With
ReDim dArr(1 To UBound(eArr, 1), 1 To 25)
For I = 1 To UBound(eArr, 1)
Tem = eArr(I, 1) & "|" & eArr(I, 4)
If Not Dic.Exists(Tem) Then
K = K + 1
Dic.Add Tem, K
dArr(K, 1) = K
For J = 1 To 4
dArr(K, J + 1) = eArr(I, J)
Next J
dArr(K, 6) = eArr(I, 12) + LuuKho
dArr(K, 7) = eArr(I, 9)
For J = 5 To 8
dArr(K, J + 3) = eArr(I, J)
Next J
dArr(K, 24) = eArr(I, 10) & "-" & eArr(I, 11)
End If
Next I
With Sheets("Nhaplieu")
sArr = .Range(.[A7], .[A7].End(xlDown)).Resize(, 15).Value
End With
For I = 1 To UBound(sArr, 1)
Tem = sArr(I, 4) & "|" & sArr(I, 13)
If Dic.Exists(Tem) Then
Rws = Dic.Item(Tem)
If sArr(I, 8) <> Empty Then
efdate = sArr(I, 1)
If efdate < eDate Then
LuuKhoT = DateDiff("m", efdate, eDate)
dArr(Rws, 6) = LuuKhoT + eArr(Dic.Item(Tem), 12)
Else
dArr(Rws, 6) = ""
End If
End If
If sArr(I, 1) < fDate Then
dArr(Rws, 8) = dArr(Rws, 8) + sArr(I, 8) - sArr(I, 9)
If sArr(I, 14) = "OK" Then
dArr(Rws, 9) = dArr(Rws, 9) + sArr(I, 8) - sArr(I, 9)
ElseIf sArr(I, 14) = "NG" Then
dArr(Rws, 10) = dArr(Rws, 10) + sArr(I, 8) - sArr(I, 9)
ElseIf sArr(I, 14) = "QA" Then
dArr(Rws, 11) = dArr(Rws, 11) + sArr(I, 8) - sArr(I, 9)
End If
ElseIf sArr(I, 1) <= eDate Then
dArr(Rws, 12) = dArr(Rws, 12) + sArr(I, 8)
dArr(Rws, 16) = dArr(Rws, 16) + sArr(I, 9)
If sArr(I, 14) = "OK" Then
dArr(Rws, 13) = dArr(Rws, 13) + sArr(I, 8)
dArr(Rws, 17) = dArr(Rws, 17) + sArr(I, 9)
ElseIf sArr(I, 14) = "NG" Then
dArr(Rws, 14) = dArr(Rws, 14) + sArr(I, 8)
dArr(Rws, 18) = dArr(Rws, 18) + sArr(I, 9)
ElseIf sArr(I, 14) = "QA" Then
dArr(Rws, 15) = dArr(Rws, 15) + sArr(I, 8)
dArr(Rws, 19) = dArr(Rws, 19) + sArr(I, 9)
End If
End If
End If
Next I
For I = 1 To K
For J = 20 To 23
dArr(I, J) = dArr(I, J - 12) + dArr(I, J - 8) - dArr(I, J - 4)
Next J
If Len(dArr(I, 24)) > 2 Then
Tmp = Split(dArr(I, 24), "-")
If dArr(I, 20) < Val(Tmp(0)) Then
dArr(I, 25) = "Min"
ElseIf dArr(I, 20) > Val(Tmp(1)) Then
dArr(I, 25) = "Max"
End If
End If
Next I
For I = 1 To K
If dArr(I, 20) = 0 Then dArr(I, 6) = Empty
Next I
Sh.[A6].Resize(K, 25).ClearContents
Sh.[A6].Resize(K, 25) = dArr
Set Dic = Nothing
End Sub