合并重复行 - VBA? [英] Consolidate Duplicate Rows - VBA?

查看:249
本文介绍了合并重复行 - VBA?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个电子表格如下:
组合重复

I have a spreadsheet as below: combine duplicates

我可以使用脚本此处

但是,我不知道将列A添加到合并列(K)。任何帮助赞赏!

However, I have no idea to add the column A to the merged column (K) . Any help appreciated!

谢谢

推荐答案

假设第1行是标题行,所以实际数据从第2行开始,您希望输出在单元格J2中启动,此代码应适用于您:

Assuming that row 1 is the header row so actual data starts on row 2, and you want the output to start in cell J2, this code should work for you:

Sub tgr()

    Dim cllSKU As Collection
    Dim SKUCell As Range
    Dim rngFound As Range
    Dim arrData(1 To 65000, 1 To 2) As Variant
    Dim strFirst As String
    Dim strJoin As String
    Dim DataIndex As Long

    Set cllSKU = New Collection

    With Range("G3", Cells(Rows.Count, "G").End(xlUp))
        On Error Resume Next
        For Each SKUCell In .Cells
            cllSKU.Add SKUCell.Text, SKUCell.Text
            If cllSKU.Count > DataIndex Then
                DataIndex = cllSKU.Count
                arrData(DataIndex, 1) = SKUCell.Text
                arrData(DataIndex, 2) = Cells(SKUCell.Row, "A").Text & " - ("
                Set rngFound = .Find(SKUCell.Text, .Cells(.Cells.Count), xlValues, xlWhole)
                If Not rngFound Is Nothing Then
                    strFirst = rngFound.Address
                    Do
                        arrData(DataIndex, 2) = arrData(DataIndex, 2) & Cells(rngFound.Row, "H").Text & ","
                        Set rngFound = .Find(SKUCell.Text, rngFound, xlValues, xlWhole)
                    Loop While rngFound.Address <> strFirst
                End If
                arrData(DataIndex, 2) = Left(arrData(DataIndex, 2), Len(arrData(DataIndex, 2)) - 1) & ")"
            End If
        Next SKUCell
        On Error GoTo 0
    End With

    If DataIndex > 0 Then
        Range("J2:K" & Rows.Count).ClearContents
        Range("J2:K2").Resize(DataIndex).Value = arrData
    End If

    Set cllSKU = Nothing
    Set SKUCell = Nothing
    Set rngFound = Nothing
    Erase arrData

End Sub

这篇关于合并重复行 - VBA?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆