如何删除重复内容并复制excel

时间:2016-10-11 01:55:51

标签: excel excel-vba excel-formula vba

请查看belwow的格式,我想删除重复的行并将最后一列内容复制到其原始行。

在下图中,我想删除重复内容行A4并将C4生活的内容复制到C3,以便C3成为 - 上帝,生命。要删除该行,A和B的内容应该相同。 在第三行,A3,B3,A4,B4是相同的,所以我们可以删除它。

在第七行和第八行中,A7和A8相同但B7和B8不同,因此不应删除。

但是在第八排,A8,A9和B8,B9也是一样的,所以第九排A9应该被删除而C8变成了婚姻,美女

非常感谢!

enter image description here

1 个答案:

答案 0 :(得分:0)

您是否考虑过如何设置逻辑?我将从A1开始并将其与剩余的A行进行比较,直到您找到空白的行。如果您发现一个与正在测试的单元格匹配的单元格开始查看B,C单元格来比较文本。如果匹配组合C单元格,则删除该行。然后步入下一个A单元格并再次执行该过程。我没有测试过这段代码,但它应该让你开始。

 Dim OuterIndex As Integer
 Dim InnerIndex As Integer

    Do While Cells(OuterIndex, 0).Value <> "" ' Loop through all the cells in Column A and stop when you find a blank one

        InnerIndex = 0 ' Reset this each time through the loop

       Do While Cells(InnerIndex, 0).Value <> "" ' Compare one cell to all the remaining, stop when you get to a blank and increment the search for the next row.

            If Cells(OuterIndex, 0).Value = Cells(InnerIndex, 0).Value Then ' This is when you find a match for the A cells

                ' Now compare the B cell for these two cells. If The B cells don't match then exit

                If Cells(OuterIndex, 1).Value = Cells(InnerIndex, 1).Value Then
                    ' Here we found a matching A and B cell so compbine the contents of the C cells in the test row and delete the second one
                    Cells(OuterIndex, 2).Value = Cells(OuterIndex, 2).Value & "," & Cells(InnerIndex, 2).Value
                    Rows(InnerIndex).Delete
                    ' We don't want to increment the InnerIndex since we need to reevaluate the same row again now that we deleted it and the rows shifted up
                    Else
                    ' B cells were different so increment to the next row
                    InnerIndex = InnerIndex + 1

                End If

            Else
            ' A cells were different so increment to the next row
            InnerIndex = InnerIndex + 1
            End If


      Loop

        OuterIndex = OuterIndex + 1 ' Increment to the next index

  Loop