我尝试根据其他列数据突出显示一列中的值。我有C1,C2列,例如:C1和C2中的8A83-AA在某些行中具有不同的Val1和Val2,如果8A83-AA(两次出现)与val1和val2数据匹配则val1高亮绿色,否则为橙色。结果应该是
我已尝试过stackoverflow中的代码
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
但是它给了我未设置的异常对象或块变量。我一次尝试这个范围一列
Set myrng = ws.Range("A2:A600")
,它有效。
但对于多列
Set myrng = ws.Range("A2:D600")
它提供了例外。
我如何使用VBA或条件格式强调上面的图片数据。
答案 0 :(得分:0)
使用一个循环来告诉val1中的单元格是否与val2相等。将你的循环放入val1并搜索val2。