Excel如何在一行中查找重复的单元格或值?

时间:2016-01-21 14:08:50

标签: excel vba excel-vba duplicates

我有一个数据集,其中每一行都属于一个独特的人,所以我想要做的是在每一行中找到重复的值。

我尝试使用条件格式,但它非常耗时,因为我必须将它应用于每个单独的行,否则它会在所有行中找到重复项而不仅仅是一行。

enter image description here

你能否建议一些可以帮助我的东西,它可以是公式或vba或条件格式的公式。

我用宏录制器创建一个宏,结果如下。如果我可以让它通过一系列行并应用可能有帮助的格式

Sub DuplicatesRow1() ' ' DuplicatesRow Macro '

'
    Rows("251:251").Select
    Selection.FormatConditions.AddUniqueValues
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    Selection.FormatConditions(1).DupeUnique = xlDuplicate
    With Selection.FormatConditions(1).Font
        .Color = -16383844
        .TintAndShade = 0
    End With
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 13551615
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
    Rows("252:252").Select
    Selection.FormatConditions.AddUniqueValues
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    Selection.FormatConditions(1).DupeUnique = xlDuplicate
    With Selection.FormatConditions(1).Font
        .Color = -16383844
        .TintAndShade = 0
    End With
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 13551615
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
    Rows("253:253").Select
    Selection.FormatConditions.AddUniqueValues
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    Selection.FormatConditions(1).DupeUnique = xlDuplicate
    With Selection.FormatConditions(1).Font
        .Color = -16383844
        .TintAndShade = 0
    End With
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 13551615
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
    Range("E259").Select End Sub

2 个答案:

答案 0 :(得分:1)

我进一步研究了这个并设法提出以下代码,这似乎对我有用。我是VBA的新手,没有足够的经验,所以如果我的代码可以进一步改进,请告诉我

Private Sub HighlightDuplicateRow(row As Integer)

Dim report As Worksheet
Set report = Excel.ActiveSheet
report.Cells(row, row).EntireRow.Select
    Selection.FormatConditions.AddUniqueValues
    Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
    Selection.FormatConditions(1).DupeUnique = xlDuplicate
    With Selection.FormatConditions(1).Font
        .Color = -16383844
        .TintAndShade = 0
    End With
    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 13551615
        .TintAndShade = 0
    End With
    Selection.FormatConditions(1).StopIfTrue = False
End Sub


Sub DuplicatesInEachRow()
Dim counter As Integer, limit As Variant
counter = 2
limit = InputBox("Give me last row number", "Highlight Duplicates in a Row")
If limit = "" Then Exit Sub
Do Until counter > limit
Call HighlightDuplicateRow(counter)
counter = counter + 1
Loop
End Sub

答案 1 :(得分:0)

这是一个循环,它将在每一行上设置条件格式。我根据您的示例数据和代码使用了工作表和范围引用。您可以修改它们以适合您的确切数据集。

我还要注意,如果行数很多,我会担心这会导致Excel中出现性能问题,因为格式化数量可能会严重影响文件大小并影响性能。

Sub LoopCF()

Dim ws As Worksheet
Set ws = Sheets("Sheet1")

'Dim lRow As Long
'lRow = ws.Range("A2").End(xlDown).Row 'will give row 200 as long as contiguous rows

Dim rng As Range, cel As Range
Set rng = ws.Range("B2:B200") 'ws.Range("B2:B" & lRow)

For Each cel In rng

    With cel.Resize(1, 4)

        .FormatConditions.AddUniqueValues
        .FormatConditions(.FormatConditions.Count).SetFirstPriority

        With .FormatConditions(1)

            .DupeUnique = xlDuplicate

            With .Font
                .Color = -16383844
                .TintAndShade = 0
            End With

            With .Interior
                .PatternColorIndex = xlAutomatic
                .Color = 13551615
                .TintAndShade = 0
            End With

            .StopIfTrue = False

        End With

    End With

Next

End Sub