|
本帖最后由 cbtaja 于 2014-4-6 18:19 编辑
- Sub 发料()
- With Sheet1.[a1].CurrentRegion
- '先按原始数据料号、批次升序排序,以便先进先出
- .Sort key1:=Range("A1"), order1:=xlAscending, _
- key2:=Range("E1"), order2:=xlAscending, Header:=xlYes
- arr = .Value
- a = UBound(arr)
- ReDim crr(1 To a, 1 To 5)
- End With
- brr = Sheet2.[a1].CurrentRegion
- b = UBound(brr)
- Set dic = CreateObject("Scripting.Dictionary")
- For i = 3 To b
- dic(brr(i, 1)) = brr(i, 3)
- Next
- For i = 2 To a
- '下面计算出货情况
- If dic.exists(arr(i, 1)) Then '当前料号有出货需求
- m = dic(arr(i, 1)) '查询总需求出货量
- chjl = chjl + 1 '预增一条出货记录
- For j = 1 To 5
- crr(chjl, j) = arr(i, j) '填写出货记录,暂假定把本储位全部出货
- Next
- '接着计算:①实际出货量、②当前储位余量
- If arr(i, 3) >= m Then '当前储位足够时
- crr(chjl, 3) = m '当前料号出货量按需发满,
- dic.Remove arr(i, 1) '然后清除当前料号出货需求记录
- arr(i, 3) = arr(i, 3) - m '当前储位余量
- Else '否则,当前储位余量不足
- crr(chjl, 3) = arr(i, 3) '出货量为当前储位数量
- arr(i, 3) = 0 '当前储位无剩余
- End If
- End If
- If arr(i, 3) > 0 Then '如当前储位确有剩余
- yhjl = yhjl + 1 '则在新库存记录中增加一条记录编号
- For j = 1 To 5
- arr(yhjl, j) = arr(i, j) '填写该储位的新库存记录
- Next
- End If
- Next
- With Sheet2
- .Range("G3:K1048576").Clear
- .[G3].Resize(chjl, 5) = crr '输出发料清单
- End With
- With Sheet1
- .Range("G2:K1048576").Clear
- .[G2].Resize(yhjl, 5) = arr '更新库存清单
- End With
- If dic.Count > 0 Then MsgBox "部分货物库存不足、未发满!", vbExclamation, "发货排料提示"
- End Sub
复制代码 |
|