|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
Sub 欠料计算()
Dim ar As Variant, cr As Variant
Dim i As Long, r As Long
Dim d As Object
Set d = CreateObject("scripting.dictionary")
Set dc = CreateObject("scripting.dictionary")
With Sheets("sheet1")
r = .Cells(Rows.Count, 1).End(xlUp).Row
.Range("p2:q" & r) = Empty
ar = .Range("a1:q" & r)
For i = 2 To UBound(ar)
s = ar(i, 8)
If s <> "" Then
If Not d.exists(s) Then Set d(s) = CreateObject("scripting.dictionary")
d(s)(i) = ""
dc(s) = ar(i, 15)
End If
Next i
For Each k In d.keys
sl = dc(k)
ys = 0: m = 0
For Each kk In d(k).keys
m = m + 1
If m = 1 Then
If ar(kk, 15) >= ar(kk, 12) Then
ar(kk, 16) = ar(kk, 12)
ys = sl - ar(kk, 12)
Else
ar(kk, 16) = ar(kk, 15)
ys = sl - ar(kk, 15)
End If
Else
If ys >= ar(kk, 12) Then
ar(kk, 16) = ar(kk, 12)
ys = ys - ar(kk, 12)
Else
If ys > 0 Then
ar(kk, 16) = ys
ys = ys - ar(kk, 12)
Else
ar(kk, 16) = 0
End If
End If
End If
ar(kk, 17) = ar(kk, 12) - ar(kk, 16)
Next kk
Next k
.Range("a1:q" & r) = ar
End With
Set d = Nothing
MsgBox "ok!"
End Sub
|
|