Excel VBAmacros条件格式与相交

我写了一个Excel VBAmacros来使用两列的相交来进行条件格式化,但是由于某种原因,我无法得到它的工作。 如果有人知道我能做些什么来解决这个问题,我会诚挚地感谢。

我想突出显示匹配或重复的源列和目标列,如下所示:

E柱(目标)0.0000%0.0000%11.1803%12.7775%13.7190%13.9841%13.9841%14.5698%14.9071%15.5746%15.6492%16.1355%16.1355%16.3123%16.3123%19.0693%19.4511%21.9089%21.9089%21.9089%

V柱(来源)13.7190%14.9240%15.4919%20.4521%21.5725%23.3319%23.7718%24.1871%25.7257%27.2166%28.2290%29.7543%29.7543%30.4968%31.0080%31.9022%32.8570%33.3333%33.3333%34.7434%34.9603%34.9927%36.4516 %36.8697%37.5637%38.2046%38.6151%38.7298%38.7298%39.3830%40.2694%41.8330%42.2049%

Sub Highlight_rsd_5batch() Dim WatchRange As Range, Target As Range, cell As Range Set Target = Range("E19:E237") 'change column ref as required Set WatchRange = Range("V19:V237") For Each cell In Target.Cells If Intersect(Target, WatchRange) Is Nothing Then cell.Interior.ColorIndex = xlNone Else: cell.EntireRow.Interior.ColorIndex = 6 End If Next cell End Sub 

Intersect函数检查两个范围是否有任何共同的单元格,而不是它们是否具有相同的值。 您可以使用CountIf函数:

 Sub Highlight_rsd_5batch() Dim WatchRange As Range, Target As Range, cell As Range Set Target = Range("E19:E237") 'change column ref as required Set WatchRange = Range("V19:V237") For Each cell In Target.Cells If Application.WorksheetFunction.CountIf(WatchRange,cell.Value) > 0 Then cell.Interior.ColorIndex = 6 For Each watchCell in WatchRange.Cells If watchCell.value = cell.Value Then: watchCell.Interior.ColorIndex = 6 Next watchCell Else: cell.EntireRow.Interior.ColorIndex = xlNone End If Next cell End Sub 

这个任务并不需要使用VBA,可以使用格式>条件格式下的条件格式工具中的相同公式来完成。 请参阅链接教程以获取更多帮助。