我编写了一个Excel VBA宏来使用两列的交叉来进行条件格式化,但由于某些原因我无法使其工作。如果有人知道我能做些什么来解决它,我会真诚地感激它。
我想突出显示匹配或重复的源列和目标列,如下所示:
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
答案 0 :(得分:0)
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,可以使用格式>条件格式下的条件格式工具中的相同公式来完成。请参阅链接的tutorial以获取更多帮助。