i = 0
For Each CEL In Range("A4", Range("A4").End(xlDown))
CEL.Offset(0, 1).FormulaArray = "=SUM((MONTH(入库登记!$C$2C$10)=MONTH(汇总!$B$2))*(入库登记!$D$2D$10=汇总!A" & (4 + i) & ")*(入库登记!$E$2E$10))"
i = i + 1
Next
Dim sumValue
Dim cel, cel2
For Each cel In Range("A4", Range("A4").End(xlDown))
sumValue = 0
For Each cel2 In Sheets("入库登记").Range("D1", Sheets("入库登记").Range("D1").End(xlDown))
If cel2.Value = cel.Value Then
If Month(cel2.Offset(0, -1).Value) = Month(Range("b2")) Then
sumValue = cel2.Offset(0, 1).Value + sumValue
End If
End If
Next
cel.Offset(0, 1).Value = sumValue
Next
另外提供不需双重循环的方法,程序比较简短:
i = 0
For Each cel In Range("A4", Range("A4").End(xlDown))
cel.Offset(0, 1).Value = Evaluate("=SUM((MONTH(入库登记!$C$2C$10)=MONTH(汇总!$B$2))*(入库登记!$D$2D$10=汇总!A" & (4 + i) & ")*(入库登记!$E$2E$10))")
i = i + 1
Next