Vba 在每个组后插入行,并基于组对列的特定内容求和

Vba 在每个组后插入行,并基于组对列的特定内容求和,vba,excel,openpyxl,Vba,Excel,Openpyxl,我有连续行中的数据,但它们在名称列中排序,即(目前为“A”,但可能会更改) 我想在每组名称后插入一行(或两行,任意数量的行),即和列'B'之和 A B C Name1 123 rdt Name1 256 efh Name1 456 ikj =sum(above number of Name1s) Name2 123 rfa =sum(above number of Name2s) Name3

我有连续行中的数据,但它们在名称列中排序,即(目前为“A”,但可能会更改)

我想在每组名称后插入一行(或两行,任意数量的行),即和列'B'之和

  A     B   C
    Name1   123 rdt
    Name1   256 efh
    Name1   456 ikj
        =sum(above number of Name1s)
    Name2   123 rfa
        =sum(above number of Name2s)
    Name3   654 xxd
    Name3   846 olp
        =sum(above number of Name3s)
    Name6   841 yuf
我也有类似的问题 AWK:在每组数据后插入一行 但是用Excel

另外,在Python中使用openpyxl是否可以做到这一点,如果可以,如何实现?求你了

谢谢你

又快又脏

Option Explicit

Sub main()
    Dim i As Long
    Dim sum As Double

    With Range("A1").CurrentRegion
        i = 1
        Do
            If .Cells(i + 1, 1) = .Cells(i, 1) Then
                sum = sum + .Cells(i, 2)
                i = i + 1
            Else
                sum = sum + .Cells(i, 2)
                i = i + 1
                .Rows(i).Insert xlDown
                .Cells(i, 2).Value = sum
                i = i + 1
                sum = 0
            End If
        Loop While i <= .Rows.Count
    End With
End Sub
选项显式
副标题()
我想我会坚持多久
双份点心
具有范围(“A1”)。当前区域
i=1
做
如果.Cells(i+1,1)=.Cells(i,1),则
总和=总和+单元格(i,2)
i=i+1
其他的
总和=总和+单元格(i,2)
i=i+1
.行(i).插入xlDown
.单元格(i,2).值=总和
i=i+1
总和=0
如果结束

循环,而我请花时间来格式正确,以更好的可读性它是美丽的格式在上传之前,但当我张贴它搞砸了。如何上传图片?请参考我的个人资料图片,以便更容易了解我的要求。请认为新用户无法链接内联图像。我想,将来,使用表的代码格式(行前4个空格)完成,但谢谢@nj2237Thank,我会在启动windows后尝试反馈,(接受您的答案并评分)。(现在在Linux上为双引导)。但这意味着没有用于此的Python代码?
Option Explicit

Sub main()
    Dim i As Long
    Dim sum As Double

    With Range("A1").CurrentRegion
        i = 1
        Do
            If .Cells(i + 1, 1) = .Cells(i, 1) Then
                sum = sum + .Cells(i, 2)
                i = i + 1
            Else
                sum = sum + .Cells(i, 2)
                i = i + 1
                .Rows(i).Insert xlDown
                .Cells(i, 2).Value = sum
                i = i + 1
                sum = 0
            End If
        Loop While i <= .Rows.Count
    End With
End Sub