循环检测一行中的重复数据

问题描述 投票:0回答:1

我录制这个宏是为了检测一行中的重复数据:

Sub Macro1()
'
' Macro1 Macro
'
    Rows("2:2").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

我的数据是这样的

1 A 栏 B 栏 C 栏 D 栏
3 苹果 梨子 香蕉 橙色
4 苹果 香蕉 苹果 梨子

我希望 Excel 检测到第 4 行中 A 列和 C 列中的数据是重复的。我不需要它来检测不同行之间的重复。

现在我希望它对所有 7252 行重复。


它与此一起工作

Dim x As Integer
For x = 2 To 7252
Rows(x).Select
(codes to detect duplicates)
Next x
excel vba loops
1个回答
0
投票
  • 使用
    Type:=xlExpression
    用公式设置 FC,那么你就不必循环遍历所有行。
Option Explicit

Sub Demo()
    Dim lastRow As Long, oFC As FormatCondition
    lastRow = ActiveSheet.Cells(ActiveSheet.Rows.count, "A").End(xlUp).Row
    Set oFC = Range("A2:D" & lastRow).FormatConditions.Add(Type:=xlExpression, Formula1:="=COUNTIF(2:2,A2)>1")
    oFC.SetFirstPriority
    With oFC.Font
        .Color = -16383844
        .TintAndShade = 0
    End With
    With oFC.Interior
        .PatternColorIndex = xlAutomatic
        .Color = 13551615
        .TintAndShade = 0
    End With
    oFC.StopIfTrue = False
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.