搁[浅灬 发表于 2012-8-8 21:24
谢谢,基本可以了,不过如果125 数量500,库存不足,再有125的话,就无法调试 - Sub test()
- Dim arr, i&, d As Object, st1$, st2$
- Dim brr, x&, j&, temp1, crr
- Set d = CreateObject("scripting.dictionary")
- Sheet2.Range("a1").CurrentRegion.Sort key1:="商品名称", order1:=xlAscending, key2:="入库时间", order2:=xlAscending, header:=xlYes
- arr = Sheet2.Range("a1").CurrentRegion
- For i = 2 To UBound(arr)
- st1 = arr(i, 1)
- st2 = i
- If Not d.exists(st1) Then
- d(st1) = st2
- Else
- d(st1) = d(st1) & "|" & st2
- End If
- Next
- brr = Sheet1.Range("A2", "c" & Sheet1.Range("a65536").End(xlUp).Row)
- ReDim crr(1 To UBound(arr) + UBound(brr), 1 To 3)
- crr(1, 1) = "商品名称": crr(1, 2) = "仓库": crr(1, 3) = "出货数量"
- y = 1
- For i = 1 To UBound(brr)
- st1 = brr(i, 1)
- st2 = ""
- If d.exists(st1) Then
- x = brr(i, 2)
- temp1 = Split(d(st1), "|")
- For j = 0 To UBound(temp1)
- If x > arr(temp1(j), 3) Then
- If arr(temp1(j), 3) > 0 Then
- If st2 = "" Then st2 = arr(temp1(j), 2) & "/" & arr(temp1(j), 3) Else st2 = st2 & ";" & arr(temp1(j), 2) & "/" & arr(temp1(j), 3)
- If j = UBound(temp1) Then MsgBox "商品" & st1 & "-库存不够": brr(i, 3) = st2
- d(st1) = Replace(d(st1), temp1(j) & "|", "", , 1)
- x = x - arr(temp1(j), 3)
- y = y + 1
- crr(y, 1) = arr(temp1(j), 1): crr(y, 2) = arr(temp1(j), 2): crr(y, 3) = arr(temp1(j), 3)
- arr(temp1(j), 1) = "": arr(temp1(j), 2) = "": arr(temp1(j), 3) = 0: arr(temp1(j), 4) = ""
- End If
- Else
- If st2 = "" Then st2 = arr(temp1(j), 2) & "/" & x Else st2 = st2 & ";" & arr(temp1(j), 2) & "/" & x
- brr(i, 3) = st2
- arr(temp1(j), 3) = arr(temp1(j), 3) - x
- y = y + 1
- crr(y, 1) = arr(temp1(j), 1): crr(y, 2) = arr(temp1(j), 2): crr(y, 3) = x
- If arr(temp1(j), 3) = 0 Then arr(temp1(j), 1) = "": arr(temp1(j), 2) = "": arr(temp1(j), 3) = 0: arr(temp1(j), 4) = ""
- Exit For
- End If
- Next
- End If
- Next
- Sheet1.Range("a2").Resize(UBound(brr), 3) = brr
- ' Sheet2.Range("a1").Resize(UBound(arr), UBound(arr, 2)) = arr
- ' Sheet2.Range("a1").Resize(UBound(arr), UBound(arr, 2)).Sort key1:="商品名称", order1:=xlAscending, key2:="入库时间", order2:=xlAscending, header:=xlYes
- Sheet3.Cells.ClearContents: Sheet3.Range("a1").Resize(y, 3) = crr
- End Sub
复制代码 |