我有一份地址列表,部分地址已被复制。例如有些记录在D栏和E栏都包含“伦敦”。
我想查找并突出显示所有列中的所有重复值,但是在同一行中。
到目前为止,我已经编写了下面的代码,但我希望它能够处理包含值的每个列,而不仅仅是我命名的两列。
Dim Lastrow As Long
Dim i As Long
Lastrow = Range("D" & Rows.Count).End(xlUp).Row
For i = 2 To Lastrow
If Range("D" & i).Value = Range("E" & i).Value Then
Range("E" & i).Interior.ColorIndex = 6
End If
Next i
我试图搜索答案,但我只能找到突出显示整个重复行或重复不同列和行中值的方法。
感谢您抽出宝贵时间阅读本文以及您可以给予的任何帮助。
答案 0 :(得分:2)
如果你真的想要一个VBA解决方案,这就可以解决问题:
Sub JustCall()
Call DuplicatedInRangeByRow(Range("A1:D5"))
End Sub
Sub DuplicatedInRangeByRow(RangeToLook As Range)
Const ColorHighlight = vbYellow
Dim ItemRange As Range
Dim TotalRows As Long: TotalRows = IIf(RangeToLook.Row > 1, RangeToLook.Rows.Count + RangeToLook.Row - 1, RangeToLook.Rows.Count)
Dim TotalCols As Long: TotalCols = IIf(RangeToLook.Column > 1, RangeToLook.Columns.Count + RangeToLook.Column - 1, RangeToLook.Columns.Count)
Dim CounterCols As Long
Dim CounterRows As Long
Dim StartCol As Long
Dim SheetForRange As Worksheet: Set SheetForRange = RangeToLook.Parent
For CounterRows = RangeToLook.Row To TotalRows
For CounterCols = RangeToLook.Column To TotalCols
StartCol = IIf(StartCol = 0, CounterCols, StartCol)
With SheetForRange
If CStr(.Cells(CounterRows, StartCol).Value) = CStr(.Cells(CounterRows, CounterCols).Value) And StartCol <> CounterCols Then .Cells(CounterRows, StartCol).Interior.Color = ColorHighlight: .Cells(CounterRows, CounterCols).Interior.Color = ColorHighlight
End With
Next CounterCols
StartCol = 0
Application.StatusBar = "Progress: " & CounterRows & " out of " & TotalRows & " Rows analyzed " & Format(CounterRows / TotalRows, "Percent")
Next CounterRows
End Sub
答案 1 :(得分:1)
对于条件格式,您将使用以下公式:
=COUNTIF($A1:$J1,A1)>1
$A1
和A1
指的是应用格式的范围中最左上角的单元格。 $J1
是范围的右上角单元格。
密切关注什么是绝对的,什么是相对的。