【问题标题】:Excel How to find duplicate cells or values in a row?Excel 如何在一行中查找重复的单元格或值?
【发布时间】:2016-04-27 19:57:18
【问题描述】:

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

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

您能否建议一些可以帮助我的东西,它可以是公式或 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

【问题讨论】:

  • 这似乎是 Excel 中的一个糟糕的设计缺陷。你确实在这里放了excel-vba 标签。您是否尝试过使用代码来完成此操作?如果是这样,请提供您尝试过的内容。否则,该问题可能会被否决并关闭。另一种选择——为每一行输入格式可能更省时——是逐行复制和粘贴格式;尽管这对于大量行数来说显然很麻烦。
  • 另外,如果您提供一个包含一些示例数据的示例来详细说明您的问题,这会增加您获得好答案的机会。
  • @ScottHoltzman Vba 代码已添加
  • @Michael 示例添加

标签: vba excel duplicates


【解决方案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

【讨论】:

    【解决方案2】:

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

    我还要注意,如果有很多行,我担心这会导致 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
    

    【讨论】:

    • 谢谢。运行此代码时出现错误。我回家后会修补它。您还可以澄清一下 lRow = ws.Range("A251").End(xlDown).Row 和 Set rng = ws.Range("B251:B" & lRow) 的用途,这两个值让我感到困惑吗?假设我想将此应用于第 2 行到第 200 行,我该怎么做?
    • @AAZ - 查看我编辑的代码。 lRow 只是获取 Range 中的最后一行,rng 将所需的范围设置为一个对象,因此它可以在该对象上工作。另外,如果你告诉我你得到的错误以及在哪一行,我可能会提供帮助:)
    • 我尝试了更新的代码,这次没有错误,但它没有做任何事情:(工作表的名称是 Sheet1。
    • 其他方法是否可以有一个循环,将格式应用于一行,然后将一个添加到 Rows("251:251").Select 从而使其成为 Rows("252:252").Select等等。它可能会询问您要应用多少行来输入数字,然后从 2 开始,直到用户定义的行号
    • @AAZ - 立即尝试。我拿出了一些我认为可能不必要的东西,但也许它是。此代码将通过循环遍历行中的每个单元格来row-by-row。不过,如果您愿意,可以使用输入框和计数器。
    猜你喜欢
    • 2014-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-05
    • 1970-01-01
    • 1970-01-01
    • 2021-01-03
    • 2011-06-14
    相关资源
    最近更新 更多