从两列中提取重复项

时间:2019-07-24 13:41:55

标签: excel vba

我想在两个单独的列中查找重复项,并将仅在这两个列中具有重复项的那些提取到另一个工作表。我想将这些列与其自身进行比较,并在同一列中查找重复项。有关如何进行此操作的任何建议。 一栏包含货币,另一栏包含文本。我添加了一张图片进行说明,但这是我到目前为止尝试过的:

Sub match()
Dim wstSource As Worksheet, _
    wstOutput As Worksheet
Dim rngMyData As Range, _
    helperRng As Range

Set wstSource = Worksheets("Sheet1")
Set wstOutput = Worksheets("Sheet2")

With wstSource
    Set rngMyData = .Range("A1:AC" & .Range("T" & .Rows.Count) & .Range(Left("N", 4) & .Rows.Count).End(xlUp).Row)
End With
Set helperRng = rngMyData.Offset(, rngMyData.Columns.Count + 1).Resize(, 1)

With helperRng
    .FormulaR1C1 = "=if(countif(C20,RC20)>1,"""",1)"
    .Value = .Value
    .SpecialCells(xlCellTypeBlanks).EntireRow.Copy Destination:=wstOutput.Cells(2, 1)
    .ClearContents
End With
End Sub

enter image description here

1 个答案:

答案 0 :(得分:0)

由于没有直接的方法可以实现此目的,因此我们需要遍历数据中的所有行。对于每一行,您需要检查是否存在另一行具有完全相同的数据。

突出显示重复项:

Sub FoundDuplicate()

    Dim lastRow As Integer, compRow As Integer, rowNo As Integer


    lastRow = Sheet1.Range("N1").CurrentRegion.Rows.Count

    For rowNo = 2 To lastRow

        For compRow = rowNo + 1 To lastRow

            If Range("N" & compRow) = Range("N" & rowNo) Then

                If Range("T" & compRow) = Range("T" & rowNo) Then

                        Range("N" & compRow & ":T" & compRow).Interior.Color = vbYellow
                        Range("N" & rowNo & ":T" & rowNo).Interior.Color = vbYellow

                End If
            End If
        Next compRow
    Next rowNo
End Sub

删除重复项:

Sub FoundDuplicate()

    Dim lastRow As Integer, compRow As Integer, rowNo As Integer


    lastRow = Sheet1.Range("N1").CurrentRegion.Rows.Count


    For rowNo = lastRow To 2 Step -1


        For compRow = rowNo - 1 To 2 Step -1


            If Range("N" & compRow) = Range("N" & rowNo) Then

                '
                If Range("T" & compRow) = Range("T" & rowNo) Then

                        Range("N" & rowNo).EntireRow.Delete

                End If
            End If
        Next compRow
    Next rowNo
End Sub

注意::等待您的反馈