Sub GiaXangDau()
Dim Data(), GiaX(), GiaD(), TinhVung(), Res()
Dim sRow&, i&, jk&
With Sheet1
Data = .Range("A2", .Range("D" & Rows.Count).End(xlUp)).Value
End With
With Sheet2
GiaX = .Range("A2", .Range("E" & Rows.Count).End(xlUp)).Value
GiaD = .Range("G2", .Range("K" & Rows.Count).End(xlUp)).Value
End With
With Sheet3
TinhVung = .Range("B3", .Range("C" & Rows.Count).End(xlUp)).Value
End With
With CreateObject("scripting.dictionary")
sRow = UBound(TinhVung)
For i = 1 To sRow
.Item(TinhVung(i, 1)) = CLng(Right(TinhVung(i, 2), 1)) + 2
Next i
sRow = UBound(Data)
ReDim Res(1 To sRow, 1 To 2)
For i = 1 To sRow
jk = .Item(Data(i, 1))
If Data(i, 4) Like "X?ng" Then
Call TinhGia(Res, jk, i, Data(i, 2), Data(i, 3), GiaX)
Else
Call TinhGia(Res, jk, i, Data(i, 2), Data(i, 3), GiaD)
End If
Next i
End With
Sheet1.Range("E2").Resize(sRow, 2) = Res
End Sub
Private Sub TinhGia(Res, jk, ByVal ik&, ByVal fDay As Date, ByVal eDay As Date, ByVal sArr)
Dim i&, sRow&
sRow = UBound(sArr)
For i = sRow To 1 Step -1
If eDay >= sArr(i, 5) Then
If fDay >= sArr(i, 5) Then
Res(ik, 1) = sArr(i, jk)
Exit For
Else
Res(ik, 2) = sArr(i, jk)
End If
End If
Next i
End Sub