合并重复行 - Excel VBA?

时间:2013-09-28 18:09:09

标签: excel excel-vba vba

我能够使用相同的SKU合并单元格并使用下面的代码删除行,但是我不知道如何在同一行中合并sku + qty。

id     sku   Qty Total Qty  Weight  Address Zip     etc...
576996  A     1      7       2.6            
576996  B     1      7       2.6            
576996  C     2      7       2.6            
576996  D     3      7       2.6            

Current code sku              Qty               
576996  A, B, C, D     1, 1, 2, 3     7  2.6



Desired                            Qty          
576996  A (1),B(1),C(2),D(3)    1, 1, 2, 3     7    2.6 


Dim sh As Worksheet, lr As Long
Set sh = Sheets(1) 'Edit sheet name
lr = sh.Cells(Rows.Count, 1).End(xlUp).Row
For i = lr To 3 Step -1
    With sh
        If .Cells(i, 1) = .Cells(i - 1, 1) Then
            If .Cells(i - 1, 2).Value <> .Cells(i, 2).Value Then
                .Cells(i - 1, 2) = .Cells(i - 1, 2).Value & ", " & .Cells(i, 2).Value
                .Cells(i - 1, 3) = .Cells(i - 1, 3).Value & ", " & .Cells(i, 3).Value
            End If
            Rows(i).Delete
        End If
    End With
Next
End Sub     

1 个答案:

答案 0 :(得分:1)

只需扩展设置sku的行

 .Cells(i - 1, 2) = .Cells(i - 1, 2).Value & ", " & _ 
     .Cells(i, 2).Value & " (" & .Cells(i, 3) & ")"