MS Excel how to create a macro to find duplicates and highlight them?

后端 未结 5 1278
臣服心动
臣服心动 2020-12-22 13:13

How can I create a macro in MS excel to find duplicates in a spreadsheet and highlight it

相关标签:
5条回答
  • 2020-12-22 13:22

    Ross Larson answered this question here: Finding duplicate rows in excel

    Quoting from his answer, "The absolute fastest and easiest way. Conditional formatting, highlight duplicates (on the ID column). Then filter the column (presumably in a table) by the coloring (above the check boxes)."

    Yesterday, I personally tried this out and it worked great. No need to write a macro or fancy VBA script. Just use the Excel out-of-the-box functionality.

    The answer Paul Rayner gave in 2010 has a broken link. The Ross Larson link is still working - at least for now.

    0 讨论(0)
  • 2020-12-22 13:26
    Sub MarkDuplicates2()
    Dim rngCell As Variant
    Dim flag As Integer
    
    Dim LastRow As Long
    
    'To Check Duplicate records for dynamic rows:
        LastRow = 0
        With ActiveSheet
        LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
        End With
    
    flag = 0`enter code here`
    'Cell(2,2) represent "B2"
    Set rng = Range(Cells(2, 2), Cells(LastRow, 2))
    iWarnColor = xlThemeColorAccent2
    
    For Each rngCell In rng.Cells
        vVal = rngCell.Text
        If (WorksheetFunction.CountIf(rng, vVal) = 1) Then
            rngCell.Interior.Pattern = xlNone
    
        Else
            rngCell.Interior.ColorIndex = iWarnColor
            flag = flag + 1
        End If
    Next rngCell
    
        If flag > 0 Then
        MsgBox flag & " cells (in light blue) contain an error.  Please Check!"
        Else
        MsgBox " Data Validation completed. No errors found."
        End If
    
    End Sub
    
    0 讨论(0)
  • 2020-12-22 13:27

    You don't need a VBA macro. You can just use conditional formatting. Microsoft explain how to do exactly what you seem to need here:

    http://office.microsoft.com/en-us/excel/HA011366161033.aspx

    If you really need a macro, the easiest way would be to record the steps described above, then edit as needed.

    0 讨论(0)
  • 2020-12-22 13:34
    Sub Macro1()
    
        Dim Counter As Integer
    
        For Counter = 1 To 35
    
            'Cells.(X,Y) X = number, Y = Letter i.e D5 Cells(5,4)
            firstValue = ActiveSheet.Cells(Counter, 3)
            SecondValue = ActiveSheet.Cells(Counter, 4)
    
            If firstValue = SecondValue Then
                Rows(Counter).Interior.Color = RGB(255, 10, 10)
            End If
    
    
        Next
    
    End Sub
    
    0 讨论(0)
  • 2020-12-22 13:45

    Maybe this snippet is useful:

    Public Sub MarkDuplicates()
    Dim iWarnColor As Integer
    Dim rng As Range
    Dim rngCell As Variant
    
    
    Set rng = Range("A1:A200") ' area to check '
    iWarnColor = xlThemeColorAccent2
    
    For Each rngCell In rng.Cells
        vVal = rngCell.Text
        If (WorksheetFunction.CountIf(rng, vVal) = 1) Then
            rngCell.Interior.Pattern = xlNone
        Else
            rngCell.Interior.ColorIndex = iWarnColor
        End If
    Next rngCell
    End Sub
    
    0 讨论(0)
提交回复
热议问题