晚上好
请参阅附件图片以获取我的数据示例。 A 列中的字符串组合在一起。
以下代码是实现以下目标的 WIP...
到目前为止,它在每个交货地点之后添加了新行,但我不知道如何添加总和公式。我知道如何添加字符串,但我不知道如何引用上面的单元格......
上面的图像是我想要实现的。
Sub insertRow_totals()
Dim changeRow, counter As Integer
counter = 2
While Cells(counter, 1) <> ""
If Cells(counter, 1) <> Cells(counter - 1, 1) Then
Rows(counter).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
counter = counter + 2
End If
counter = counter + 1
Wend
Rows(2).EntireRow.Delete
End Sub
最佳答案
您需要计算有多少具有相同名称的行(或记住第一行的行索引),然后这样的事情应该可以工作
Sub insertRow_totals()
Dim changeRow, counter As Integer
counter = 2
FirstRow = 2
While Cells(counter, 1) <> ""
If Cells(counter, 1) <> Cells(counter - 1, 1) Then
Rows(counter).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
For i = 11 To 14
ActiveSheet.Cells(counter, i).Formula = "=SUM(" & Cells(FirstRow, i).Address & ":" & Cells(counter - 1, i).Address & ")"
Next i
counter = counter + 1
FirstRow = counter
End If
counter = counter + 1
Wend
Rows(2).EntireRow.Delete
End Sub
关于Excel VBA - 添加行并激活,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30877131/