MS Excel如何创建一个宏来查找重复项并突出显示它们?

时间:2010-01-29 14:53:13

标签: excel

如何在MS excel中创建宏以在电子表格中查找重复项并突出显示

5 个答案:

答案 0 :(得分:2)

您不需要VBA宏。您可以使用条件格式。 Microsoft解释了如何完全按照您的需要进行操作:

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

如果你真的需要一个宏,最简单的方法是记录上述步骤,然后根据需要进行编辑。

答案 1 :(得分:2)

也许这段代码很有用:

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

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

答案 4 :(得分:0)

Ross Larson在这里回答了这个问题:Finding duplicate rows in excel

引用他的回答,"绝对最快,最简单的方式。条件格式化,突出显示重复项(在ID列上)。然后通过着色(在复选框上方)过滤色谱柱(可能在表格中)。"

昨天,我个人尝试了这个,效果很好。无需编写宏或花哨的VBA脚本。只需使用Excel开箱即用的功能。

保罗雷纳在2010年给出的答案有一个断开的链接。 Ross Larson链接仍然有效 - 至少目前是这样。