【问题标题】:VBA highlighting duplicates in a columnVBA突出显示列中的重复项
【发布时间】:2021-02-09 07:08:15
【问题描述】:

我有一列 Excel 数据,我试图突出显示重复值。当我运行 VBA 代码时,它会突出显示每个值(我知道有些值只列出一次)。我的数据从 b 列的第 4 行到第 45605 行以供参考。代码如下:

Sub sbHighlightDuplicatesInColumn()

    Dim lastRow As Range
    Dim matchFoundIndex As Long
    Dim iCntr As Long
    
    
    'makes sure to select the correct sheet to work in
Sheets("repeat offenders").Select

'defines the last row
    'lastRow = Cells(45605, 2)


For iCntr = 4 To 45605
    If Cells(iCntr, 2) <> "" Then
        matchFoundIndex = WorksheetFunction.Match(Cells(iCntr, 2), Range("B4:B45605"), 0)
        If iCntr <> matchFoundIndex Then
        Cells(iCntr, 1).Interior.Color = vbYellow
        End If
        End If
Next iCntr
      
End Sub

【问题讨论】:

    标签: vba duplicates


    【解决方案1】:

    “样式”部分的“主页”选项卡上有一个选项,称为条件格式。您可以选择要应用此功能的单元格,然后单击条件格式 > 突出显示单元格规则 > 重复值。将弹出一个对话框,允许您更改突出显示颜色和其他一些选项。希望有帮助。如果你仍然想使用这种方式,你必须在你的第二个 if 语句中添加 3 到 matchFoundIndex。这使它部分工作,但您还需要从匹配功能中排除当前选定的单元格。匹配从顶行开始,因此每个重复项的第一个实例不会突出显示。

    【讨论】:

    • @Meg 如果这对您有用,请点击复选标记以接受答案。
    猜你喜欢
    • 2014-02-19
    • 2016-09-07
    • 2020-05-02
    • 2019-07-02
    • 1970-01-01
    • 2021-03-25
    • 2019-11-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多