根据多列中的重复值复制行

时间:2014-09-04 16:45:44

标签: excel vba

我有下表,如果订单ID和合同相同,我想复制行

a busy cat http://i57.tinypic.com/2qta42b.jpg

我想让宏做到这一点:

a busy cat http://i59.tinypic.com/2vdfc0i.png

你们能帮我一个正确的方向来实现这个目标吗?提前谢谢

1 个答案:

答案 0 :(得分:0)

您可以提高此代码的效率,但这是“向正确的方向努力实现此目标”:)

Sub myMacro()

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' decleration

    Dim rowData As Integer, rowNew As Integer
        rowData = 1
        rowNew = 1
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' put first two entries

    Range("H" & rowNew, "J" & rowNew).Value = Range("A" & rowData, "C" & rowData).Value
    rowNew = rowNew + 1
    rowData = rowData + 1

    Range("H" & rowNew, "J" & rowNew).Value = Range("A" & rowData, "C" & rowData).Value
    rowNew = rowNew + 1
    rowData = rowData + 1
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' loop through all of column A entries

    Do While Range("A" & rowData).Value <> ""

        ' similar entry found
        If Range("A" & rowData).Value = Range("A" & rowData - 1).Value Then

            Range("H" & rowNew, "J" & rowNew).Value = Range("A" & rowData, "C" & rowData).Value
            rowNew = rowNew + 1
            rowData = rowData + 1

        ' similar entry Not found
        Else

            rowNew = rowNew + 1
            Range("H" & rowNew, "J" & rowNew).Value = Range("A1:C1").Value
            rowNew = rowNew + 1

            Range("H" & rowNew, "J" & rowNew).Value = Range("A" & rowData, "C" & rowData).Value
            rowNew = rowNew + 1
            rowData = rowData + 1

        End If

    Loop
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

End Sub