【发布时间】:2016-07-03 00:34:52
【问题描述】:
我希望你能帮助我解决我的 VBA 问题。我想使用沿着 A 列向下的循环。一旦检测到更改,我想在 C 列中插入一个 SUMIF 公式,如果将 A 列分组(向右的总偏移量)分组到 B 列的总数。 A 已经排序。有点像小计,但不使用小计行。
A B C
1 2
1 6
1 3 11 =SUMIF(A:A,A3,B:B)
2 7
2 8 15 =SUMIF(A:A,A5,B:B)
3 8
3 6 14 =SUMIF(A:A,A7,B:B)
(没有在 1 & 2 和 2 & 3 更改之间添加空白行)我相信我是其中的一部分,使用以下代码片段,但无法一起工作。
Sub SUMIF_Upon_Change()
Dim r As Long, mcol As String, i As Long
' find last used cell in Column A
r = Cells(Rows.Count, "A").End(xlUp).Row
' get value of last used cell in column A
mcol = Cells(r, 1).Value
' insert rows by looping from bottom
For i = r To 2 Step -1
If Cells(i, 1).Value <> mcol Then
Cells(n, 3).Formula = _
"=SUMIF(A:A,RC[-1],B:B)"
'AND / OR This added at the change row minus one row.
'Places formula in each adjacent cell (in column "D").
Range("C2:C" & Range("A" & Rows.Count).End(xlUp).Row).Formula = "=SUMIF(A:A,A3,BB)"
End If
Next i
End Sub
任何帮助将不胜感激。
【问题讨论】: