|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
- Sub 先进先出计算出库()
- Range("H1:L10000") = ""
- Dim m, n, x, y, z, i, j, arr, brr, ar, br(), a, b, ai, s
- Dim d As Object, k, t
- Set d = CreateObject("scripting.dictionary")
- ar = Range("A2").CurrentRegion
- With Sheet1
- arr = .Range("A3:D" & UBound(ar) + 4)
- .[H1:K10000] = ""
- .[H1].Resize(UBound(arr), 4) = arr
- .Range("H1:K" & UBound(arr) + 1).Sort Key1:=.Range("I1"), Key2:=.Range("H1")
- arr = .Range("H1").CurrentRegion
- .Range("H1:K10000") = ""
- End With
- For i = 1 To UBound(arr)
- If Not d.exists(arr(i, 2)) Then
- m = 1: ReDim brr(1 To 4, 1 To m)
- Else
- brr = d(arr(i, 2))
- m = UBound(brr, 2) + 1: ReDim Preserve brr(1 To 4, 1 To m)
- End If
- For j = 1 To 4: brr(j, m) = arr(i, j): Next
- d(arr(i, 2)) = brr
- Next
- k = d.keys: t = d.items
- For i = 2 To UBound(ar)
- For x = 0 To d.Count - 1
- If ar(i, 2) = k(x) Then
- If t(x)(3, 1) >= ar(i, 3) Then
- n = n + 1: ReDim Preserve br(1 To 5, 1 To n)
- br(1, n) = ar(i, 1)
- br(2, n) = k(x)
- br(3, n) = ar(i, 3)
- br(4, n) = t(x)(4, 1)
- br(5, n) = t(x)(1, 1)
- t(x)(3, 1) = t(x)(3, 1) - ar(i, 3)
- Else
- a = 0
- For y = 1 To UBound(t(x), 2)
- a = a + t(x)(3, y)
- If a >= ar(i, 3) Then ai = y: Exit For
- Next
- If a < ar(i, 3) Then MsgBox "【" & k(x) & "】计划出库数 【" & _
- ar(i, 3) & "】超过库存量【" & a & "】了!", vbOKCancel, "提示:": Exit Sub
- b = 0
- For y = 1 To ai - 1
- b = b + t(x)(3, y)
- If t(x)(3, y) <> 0 Then
- n = n + 1: ReDim Preserve br(1 To 5, 1 To n)
- For z = 2 To 4
- br(z, n) = t(x)(z, y)
- Next
- br(1, n) = ar(i, 1)
- br(5, n) = t(x)(1, y)
- End If
- t(x)(3, 1) = 0
- Next
- n = n + 1: ReDim Preserve br(1 To 5, 1 To n)
- br(1, n) = ar(i, 1)
- br(2, n) = t(x)(2, ai)
- br(3, n) = ar(i, 3) - b
- br(4, n) = t(x)(4, ai)
- br(5, n) = t(x)(1, ai)
- t(x)(3, ai) = a - ar(i, 3)
- End If
- End If
- Next
- Next
- s = Range("A2:E2")
- [H2].Resize(1, 5) = s: [H3].Resize(n, 5) = Application.Transpose(br)
- Set d = Nothing
- End Sub
复制代码 |
|