|
本帖最后由 qy1219no2 于 2014-11-23 00:53 编辑
试试这个代码。结果放在53列。
其实也是使用SUMIFS函数,只是先对“货号”进行排序后,缩小了函数计算范围(仅对相同货号的区域进行条件求和),如果能知道每种“货号”最多可能出现的次数,则代码还可以进一步提高运算速度。- Sub macSumIfs()
- Dim nR&, i&, j%, sj()
- nR = Cells(Rows.Count, 1).End(xlUp).Row
- If nR < 4 Then Exit Sub
- ReDim sj(nR - 4, 0)
- Application.ScreenUpdating = False
- With [iv4].Resize(nR - 3, 1)
- .Formula = "=Row()-3"
- .Value = .Value
- End With
- [a4].Resize(nR - 3, 256).Sort key1:=[a4].Resize(nR - 3, 1)
- i = 4
- Do While i <= nR
- nRec = Application.CountIf(Range(Cells(i, 1), Cells(nR, 1)), Cells(i, 1))
- For j = 1 To nRec
- sj(i + j - 5, 0) = Application.SumIfs(Cells(i, 41).Resize(nRec, 1), _
- Cells(i, 2).Resize(nRec, 1), Cells(i + j - 1, 2), _
- Cells(i, 52).Resize(nRec, 1), "<=" & Cells(i + j - 1, 52))
- Next
- i = i + nRec
- Loop
- [ba4].Resize(nR - 3, 1) = sj
- [a4].Resize(nR - 3, 256).Sort key1:=[iv4].Resize(nR - 3, 1)
- [iv1].EntireColumn.Delete
- Application.ScreenUpdating = True
- End Sub
复制代码 |
|