Sub test3()
Dim A, B, i
Sheets(1).Select
A = Range("a1:f" & Range("a65536").End(xlUp).Row)
Sheets(2).Select
B = Range("b2:b" & Range("b65536").End(xlUp).Row)
Set d = CreateObject("scripting.dictionary")
For i = 2 To UBound(A)
If A(i, 6) = "上月领料" Then d(A(i, 1)) = d(A(i, 1)) + A(i, 4)
Next i
For i = 1 To UBound(B)
B(i, 1) = d(B(i, 1)) * -1
Next i
Sub test()
Dim A, B, j, i, s
Sheets(1).Select
A = Range("a1:f" & Range("a65536").End(xlUp).Row)
Sheets(2).Select
B = Range("b2:b" & Range("b65536").End(xlUp).Row)
For i = 1 To UBound(B)
s = 0
For j = 2 To UBound(A)
If B(i, 1) = A(j, 1) And A(j, 6) = "上月领料" Then s = s + A(j, 4)
Next j
B(i, 1) = s * -1
Next i
Range("n:n").Clear
[n2].Resize(i - 1) = B
End Sub