突出显示重复项时exception

我试图根据另一列数据突出显示一列中的值。 我有C1,C2列,例如:C1和C2中的8A83-AA在一些行中具有不同的Val1和Val2,如果与val1和val2数据相匹配的8A83-AA(两个出现)则val1以绿色或橙色突出显示。 结果应该看起来像

在这里输入图像说明

我已经尝试了在计算器中的代码

Sub Highlight_Duplicate_Entry() Dim ws As Worksheet Dim cell As Range Dim myrng As Range Dim clr As Long Dim lastCell As Range Set ws = ThisWorkbook.Sheets("Sheet1") Set myrng = ws.Range("A2:d" & Range("A" & ws.Rows.Count).End(xlUp).Row) With myrng Set lastCell = .Cells(.Cells.Count) End With myrng.Interior.ColorIndex = xlNone clr = 3 For Each cell In myrng If Application.WorksheetFunction.CountIf(myrng, cell) > 1 Then ' addresses will match for first instance of value in range If myrng.Find(what:=cell, lookat:=xlWhole, MatchCase:=False, after:=lastCell).Address = cell.Address Then ' set the color for this value (will be used throughout the range) cell.Interior.ColorIndex = clr clr = clr + 1 Else ' if not the first instance, set color to match the first instance cell.Interior.ColorIndex = myrng.Find(what:=cell, lookat:=xlWhole, MatchCase:=False, after:=lastCell).Interior.ColorIndex End If End If Next End Sub 

但它给我的exception对象或块variables没有设置。 我一次尝试一列这个范围Set myrng = ws.Range("A2:A600") ,它的工作原理。 但对于多列Set myrng = ws.Range("A2:D600")它是给予例外。 而我如何突出显示上面的图片数据与VBA或条件格式。

使用一个循环来告诉val2中的单元格val2是否相等。 把你的循环val1和searchval2。