|
- Sub test()
- Dim r%, i%
- Dim arr, brr
- Dim d As Object
- Application.ScreenUpdating = False
- Application.DisplayAlerts = False
- Set d = CreateObject("scripting.dictionary")
- With Worksheets("12月采购")
- r = .Cells(.Rows.Count, 1).End(xlUp).Row
- arr = .Range("a2:h" & r)
- For i = 1 To UBound(arr)
- If Not d.exists(arr(i, 1)) Then
- m = 1
- ReDim brr(1 To 2, 1 To m)
- Else
- brr = d(arr(i, 1))
- m = UBound(brr, 2) + 1
- ReDim Preserve brr(1 To 2, 1 To m)
- End If
- brr(1, m) = arr(i, 4)
- brr(2, m) = arr(i, 5)
- d(arr(i, 1)) = brr
- Next
- End With
- With Worksheets("12月销售")
- r = .Cells(.Rows.Count, 1).End(xlUp).Row
- arr = .Range("a2:s" & r)
- ReDim crr(1 To UBound(arr), 1 To 1)
- For i = 1 To UBound(arr)
- ' If i = 30 Then
- ' Stop
- ' End If
- sl = arr(i, 3)
- cb = 0
- If d.exists(arr(i, 1)) Then
- brr = d(arr(i, 1))
- For k = 1 To UBound(brr, 2)
- If brr(1, k) > 0 Then
- If brr(1, k) >= sl Then
- cb = cb + sl * brr(2, k)
- brr(1, k) = brr(1, k) - sl
- sl = 0
- ElseIf brr(1, k) < sl Then
- cb = cb + brr(1, k) * brr(2, k)
- sl = sl - brr(1, k)
- brr(1, k) = 0
- End If
- d(arr(i, 1)) = brr
- End If
- If sl = 0 Then
- Exit For
- End If
- Next
- End If
- crr(i, 1) = cb
- Next
- .Range("g2").Resize(UBound(crr), 1) = crr
- End With
- End Sub
复制代码 |
|