突出显示后续列中重复的值

时间:2014-11-18 21:25:00

标签: vba

如果某个单元格的值在每个其他列中都有,则我希望该单元格变为高亮显示。

以下是我可以开展工作的代码部分。我已尝试嵌套其他 For-loops 来表示新列,但这不起作用,因为它计算原始列中的单元格,最后突出显示所有内容。我还将 If 行更改为:

If Cells(i,1)=Cells(j,2)=Cells(k,3) Then

但这也不起作用。以下是代码:

Sub check()
    For i = 1 To 10
        For j = 1 To 10
            If Cells(i, 1) = Cells(j, 2) Then
                Cells(i, 1).Select
                With Selection.Interior
                    .Pattern = xlSolid
                    .PatternColorIndex = xlAutomatic
                    .ThemeColor = xlThemeColorAccent6
                    .TintAndShade = -0.249977111117893
                End With
            End If
        Next j
    Next i
End Sub

1 个答案:

答案 0 :(得分:0)

Sub check()
    Dim LastCol As Long
    Dim LastRow As Long
    Dim chkRng As Range
    Dim Srch As String
    Dim CurCol As Long

        LastRow = Range("A" & Rows.Count).End(xlUp).Row
        LastCol = Cells(1, Columns.Count).End(xlToLeft).Column

        Set chkRng = ActiveSheet.Range(Cells(1, 1), Cells(LastRow, LastCol))
        For CurCol = 1 To LastCol
            For CurRow = 1 To LastRow
            Srch = Cells(CurRow, CurCol).Value
            Cells(CurRow, CurCol).Value = ""
            If Not chkRng.Find(Srch, LookIn:=xlValues, lookat:=xlWhole) Is Nothing Then
                With Cells(CurRow, CurCol).Interior
                    .Pattern = xlSolid
                    .PatternColorIndex = xlAutomatic
                    .ThemeColor = xlThemeColorAccent6
                    .TintAndShade = -0.249977111117893
                End With
                Else
            End If
            Cells(CurRow, CurCol).Value = Srch
            Next CurRow
        Next CurCol

End Sub