如果要在Excel列中检查匹配值

时间:2014-10-01 12:37:45

标签: excel vba excel-vba

我想使用If statementVBA代码)检查给定数字参数的列中的单元格范围。对于与给定值匹配的单元格,右侧(同一行)中的单元格应更改背景颜色。

伪代码示例:

A1=5,7  
If cell in Range(F1:F10) has value=A1 Then  
(random matched cell: F7=5,7)  
Range (G7:M7) = Background Blue  

改变背景的部分我知道怎么做,但检查给定范围的最佳方法是什么?

2 个答案:

答案 0 :(得分:0)

我猜你在F1中可能有多行:F10在A1上有匹配。我将遍历以下范围内的单元格:

For each rngCell in Range("F1:F10")
     If rngCell.value = Range("A1").value
          Range("G" & rngCell.row, "M" & rngCell.row).Interior.ColorIndex = 5
     End If
Next

答案 1 :(得分:0)

我想你想要像

这样的东西
for i = 1 to 10 'rows in column f to loop through
  if cells(i,6) = cells(1,1) then 'column a is 1, column f is 6, etc.
    range(cells(i,7), cells(i,13)).interior.colorindex = 'number for that color
  end if
next i