我正在尝试让VBA查看一列中的值,然后检查另一列中是否存在相同的值。
然后,我尝试高亮显示两列中显示相同值的相同数量的单元格,这意味着如果相同值在一列中显示的次数与在另一列中显示的次数不同,我需要高亮显示每列中相同数量的单元格,而不高亮显示任何“多余”的重复值。
这张图片说明了我试图完成的任务。EXCEL SCREENSHOT
如图所示,这些值已被突出显示到它们在任一列中显示的程度,而其他重复值未被突出显示。
我尝试了这段代码,但它不起作用,并突出显示了我不希望得到突出显示的单元格。我试图循环通过列,并忽略已经突出显示的单元格。
Sub highlightMatchingValues()
'Declare variables
Dim cellC As Range, cellE As Range
'Loop through each cell with a value in column C
For Each cellC In Range("C:C").Cells
If Not IsEmpty(cellC) And cellC.Interior.ColorIndex = xlNone Then 'ignore empty cells and cells that are already highlighted
'Loop through each cell with a value in column E
For Each cellE In Range("E:E").Cells
If Not IsEmpty(cellE) And cellE.Interior.ColorIndex = xlNone Then 'ignore empty cells and cells that are already highlighted
If cellC.value = cellE.value Then 'check for a match
'Highlight both cells green
cellC.Interior.Color = vbGreen
cellE.Interior.Color = vbGreen
End If
End If
Next cellE
End If
Next cellC
End Sub
1条答案
按热度按时间4nkexdtk1#
这里有一个可以解决您问题的解决方案