- Sub 先进先出计算结存()
- Dim m, n, x, i, j, arr, brr, ar, br(), a, b, c, ai, s
- Dim d As Object, k, t
- Set d = CreateObject("scripting.dictionary")
- ar = Range("A1").CurrentRegion
- arr = Range("D2:G" & [D65536].End(3).Row)
- [W1].Resize(UBound(arr), 4) = arr
- Range("W1:Z" & UBound(arr) + 1).Sort Key1:=Range("X1"), Key2:=Range("W1")
- arr = Range("W1").CurrentRegion
- Range("P1:Z10000") = "": s = Range("I2:N2")
- 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 x = 0 To d.Count - 1
- n = n + 1: ReDim Preserve br(1 To 6, 1 To n)
- br(1, n) = k(x)
- b = 0
- For i = 2 To UBound(ar)
- If ar(i, 1) = k(x) Then br(2, n) = ar(i, 2): b = ar(i, 2)
- Next
- a = 0
- For i = UBound(t(x), 2) To 1 Step -1
- a = a + t(x)(4, i)
- If a >= b Then ai = i: Exit For
- Next
- If a = b Then
- For i = UBound(t(x), 2) To ai Step -1
- For j = 1 To 4: br(j + 2, n) = t(x)(j, i): Next
- n = n + 1: ReDim Preserve br(1 To 6, 1 To n)
- Next
- Else
- c = 0
- For i = UBound(t(x), 2) To ai + 1 Step -1
- For j = 1 To 4: br(j + 2, n) = t(x)(j, i): Next
- c = c + t(x)(4, i)
- n = n + 1: ReDim Preserve br(1 To 6, 1 To n)
- Next
- For j = 1 To 4: br(j + 2, n) = t(x)(j, ai): Next
- If c + t(x)(4, i) < b Then
- n = n + 1: ReDim Preserve br(1 To 6, 1 To n)
- br(3, n) = "以前年度": br(4, n) = k(x): br(6, n) = b - c - t(x)(4, i)
- Else
- br(6, n) = b - c
- End If
- End If
- Next
- [p2].Resize(1, 6) = s: [p3].Resize(n, 6) = Application.Transpose(br)
- Set d = Nothing
- End Sub
复制代码 |