检查一个单元格中的重复副本

时间:2015-12-10 09:11:21

标签: excel excel-vba vba

我有这个代码来检查重复项,如果在单元格L中找到重复项(或更多),是否可以将K列中的单元格中的值复制到一个单元格中?

Sub check_duplicates()
    Dim x As Long
    Dim LastRow As Long
    Dim rng As String


    LastRow = Range("L65536").End(xlUp).Row
    For x = LastRow To 1 Step -1
        If Application.WorksheetFunction.CountIf(Range("L2:L" & x), Range("L" & x).Value) > 1 Then
            Range("L" & x).Copy
        End If
    Next x

End Sub

1 个答案:

答案 0 :(得分:0)

我希望你能做什么,让我知道。

Before code:

After

Sub Test()
Dim lastrow As Long


lastrow = Range("L" & Rows.Count).End(xlUp).Row

For i = 2 To lastrow

lastrow = Range("L" & Rows.Count).End(xlUp).Row

    For j = i + 1 To lastrow

            If Range("L" & j).Value = Range("L" & i).Value Then

                    If Not IsEmpty(Range("K" & i)) Then

                        Range("K" & i) = Range("K" & i) & "," & " " & Range("L" & j)
                        Rows(j).EntireRow.Delete

                     Else

                        Range("K" & i) = Range("L" & j)

                    End If

                j = j - 1

            End If

    Next j

Next i

End Sub