Excel合并类似的行和求和单元格

时间:2016-10-27 15:34:18

标签: excel excel-vba vba

合并相似行(仅订单号字母不同a4; a6; a8和生产数量)和总和(生成数量e4; e6; e8)单元格的最佳方法是什么?这就是excel表的外观  enter image description here

澄清: 这是我正在寻找的输出 enter image description here

第4行;第6行; 8除了订单列(在6和8上添加一个字母)和生产列(不同的生产数量)之外是相同的。第4,6,8行合并,生产数量相加。第6,8行被隐藏或删除。

2 个答案:

答案 0 :(得分:2)

以下示例可以解决您的问题:

Sub test()

i = 1
produced = 0
While Cells(i, 1) <> "" Or Cells(i + 1, 1) <> ""
    If Cells(i, 1) <> "" Then

        produced = Cells(i, 5)

        j = 1
        'second loop to add up every line with the same order, then suppress the lines
        While Cells(j, 1) <> "" Or Cells(j + 1, 1) <> ""
            If Left(Cells(j, 1), 7) = Left(Cells(i, 1), 7) And i <> j Then
                produced = produced + Cells(j, 5)
                Cells(j, 5).EntireRow.Select
                Selection.Delete Shift:=xlUp
                j = j - 1
            End If

            j = j + 1
        Wend

    End If

i = i + 1
Wend

答案 1 :(得分:0)

好的,这是修改过的@Bitoubi代码,它帮助了我:

Sub RemoveSplitOrders()
i = 1
produced = 0
While Cells(i, 1) <> "" Or Cells(i + 1, 1) <> ""
    If Cells(i, 1) <> "" Then

        produced = Cells(i, 20)

        j = 1
        'second loop to add up every line with the same order, then suppress the lines
        While Cells(j, 1) <> "" Or Cells(j + 1, 1) <> ""
            If Left(Cells(j, 1), 8) = Left(Cells(i, 1), 8) Or Left(Cells(j, 1), 9) = Left(Cells(i, 1), 9) Then
                If Cells(j, 2) = Cells(i, 2) And i <> j Then
                    produced = produced + Cells(j, 20)
                    Cells(i, 20).Value = produced
                    Range(Cells(j, 20), Cells(j + 1, 20)).EntireRow.Delete Shift:=xlUp
                    j = j - 1
                End If
            End If

            j = j + 1
        Wend

    End If

i = i + 1
Wend
End Sub