Dim s1 As Long, s2 As Long
Dim cindex As Long
With Sheets("出貨日")
Set Rng = .Range(.[a1], .[a1].End(xlToRight).End(xlDown).Offset(, -1)) '[出貨日]資料範圍
End With
For Each c In Sheets("交期").Range("E2:E" & Sheets("交期").[a1].End(xlDown).Row) '[交期]資料填入範圍
If c.Offset(, -4).Value <> c.Offset(-1, -4).Value Then
cindex = 1
s1 = 0 '累積至前一批數量
s2 = 0 '累積出貨需求數量
Else
s1 = s1 + c.Offset(-1, -1).Value
End If
Do While s2 <= s1
cindex = cindex + 1
If Application.IsError(Application.VLookup(c.Offset(, -4).Value, Rng, cindex, False)) Then
Exit Do
Else
s2 = s2 + Application.VLookup(c.Offset(, -4).Value, Rng, cindex, False)
End If
Loop
If s2 <= s1 Then
c.Value = "NA"
Else
c.Value = Rng.Cells(1, cindex).Value
End If
Next
Set Rng = Nothing
'在E欄填入交期作者: r1145kimo 時間: 2015-9-10 04:56