Sub aTry() Dim i%, j% Dim dt1 As Date, dt2 As Date, bz%, cnt%, ct1, ct2 Dim arrs, arrd dt1 = [b1] dt2 = [d1] bz = [b2] arrs = Sheets("数据源").Range("A2:F" & Sheets("数据源").[a65536].End(xlUp).Row) ReDim arrd(1 To UBound(arrs), 1 To 4) ct1 = 0 ct2 = 0 cnt = 0 For i = 1 To UBound(arrs) If arrs(i, 1) < dt1 Or arrs(i, 1) > dt2 Or arrs(i, 3) <> bz Then GoTo nxt For j = 1 To cnt If arrs(i, 4) = arrd(j, 1) Then Exit For Next j If j > cnt Then cnt = j arrd(j, 1) = arrs(i, 4) End If If arrs(i, 2) = "D生产领货" Then arrd(j, 2) = arrd(j, 2) + arrs(i, 6) arrd(j, 4) = arrd(j, 4) + arrs(i, 6) ct1 = ct1 + arrs(i, 6) ElseIf arrs(i, 2) = "E车间成品" Then arrd(j, 3) = arrd(j, 3) + arrs(i, 6) arrd(j, 4) = arrd(j, 4) - arrs(i, 6) ct2 = ct2 + arrs(i, 6) End If nxt: Next i [a4:d10] = arrd [b11] = ct1 [c11] = ct2 [d11] = ct1 - ct2 End Sub |