我有一份包含进货产品和它们到达的集装箱的电子表格。我想合并包含重复项目的行,同时保留每个集装箱列中该项目的数量。为此,我一直使用以下 VBA 代码:
Sub combine()
Dim c As Range
Dim i As Integer
For Each c In Range("A2", Cells(Cells.SpecialCells(xlCellTypeLastCell).Row, 1))
If c = c.Offset(1) And c <> "" Then
For i = 1 To 6
If c.Offset(1, i) <> "" Then
c.Offset(, i) = c.Offset(1, i)
End If
Next
c.Offset(1).EntireRow.Delete
End If
Next
End Sub
此代码运行良好,但是,有时这些项目在同一个容器中有多行数量。此代码只是将上面的数量替换为下面的数量。我需要将它们添加。
答案1
解决了!!!
Sub combine()
Dim c As Range
Dim i As Integer
For Each c In Range("A2", Cells(Cells.SpecialCells(xlCellTypeLastCell).Row, 1))
If c = c.Offset(1) And c <> "" Then
For i = 1 To 15
If c.Offset(1, i) <> "" Then
If c.Offset(, i).Value <> "" Then
c.Offset(, i) = c.Offset(, i) + c.Offset(1, i)
Else
c.Offset(, i) = c.Offset(1, i)
End If
End If
Next
c.Offset(1).EntireRow.Delete
End If
Next
End Sub