2009-09-28 100 views
1

我写了一个Excel VBA宏来使用两列的相交来进行条件格式化,但是由于某些原因我无法使其工作。如果任何人有任何想法我可以做什么来解决它,我会真诚地感谢它。Excel VBA宏条件格式与相交

我要突出显示的源,并且其中存在匹配靶塔两者或重复如下:

ë柱(目标) 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

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获取更多帮助。

+0

马克,感谢您花时间回答我的问题。对此,我真的非常感激。这是我编写Excel宏和学习编程的第一步。你的代码有语法错误;特别是cell.Value:所以我将“:”更改为“Then”,但现在我在“Next watchCell”中出现编译错误,因为它显示“Next Without For”。我不明白,因为上面有一个For语句。 – 2009-09-28 15:57:32

+0

我已经对代码进行了编辑。它应该是一个'然后:'如果你想在同一行上声明。否则它会抛出一个错误,因为在Next之前没有关闭'EndIf'。 – 2009-09-28 16:02:33

+0

工作得很漂亮!非常感谢你。 – 2009-09-28 16:08:34