可以使用以下代码来解决这个问题:
Sub HighlightDuplicates() Dim lastRow As Long Dim rng As Range Dim dict As Object Dim key As Variant
Set dict = CreateObject("Scripting.Dictionary") lastRow = Cells(Rows.Count, "A").End(xlUp).Row Set rng = Range("A1:B" & lastRow)
For Each cell In rng.Rows If Not dict.exists(cell.Value) Then dict.Add cell.Value, cell.Row Else If cell.Offset(0, 1) <> "specific value" Then Range("A" & dict(cell.Value) & ":B" & dict(cell.Value)).Interior.ColorIndex = 37 Range("A" & cell.Row & ":B" & cell.Row).Interior.ColorIndex = 37 End If End If Next cell
End Sub
该代码使用了一个字典对象,对于列A中的每个唯一值,将其行号存储到字典中。当遇到重复值时,如果在列B中没有指定的特定值,则突出显示这两行。请务必将代码中的“specific value”替换为列B中的实际特定值。