假设 Range 是连续的并且是单列。
我想突出显示上述范围的重复条目。我的 VBA 如下,但未按预期运行。主题是使用 OFFSET 将第一个单元格值与其底部值进行比较
Sub CompareSingleRange()
Dim rangeToUse1 As Range, rangeToUse2 As Range, cell1 As Range, cell2 As Range
' Assume Selection is contiguous
Set rangeToUse1 = Selection
Set rangeToUse2 = Selection
For Each cell1 In rangeToUse1
For Each cell2 In rangeToUse2
If cell1.Value = cell2.Offset(1,0).Value Then
cell1.Interior.ColorIndex = 38
End If
Next cell2
Next cell1
End Sub
最佳答案
尝试条件格式规则。如果看起来更容易,请对其进行编码。
With Worksheets("Sheet1")
With .Range(.Cells(2, 1), .Cells(Rows.Count, 1).End(xlUp))
.FormatConditions.Delete
.FormatConditions.AddUniqueValues
With .FormatConditions(1)
.Interior.Color = vbGreen
End With
End With
End With
关于vba - 突出显示单个范围内的重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36248131/