Excel VBA 更改单元格颜色
Excel VBA Changing Cell Color
我正在尝试获取 U 列中不等于 2.04 或 3.59 的所有单元格以更改单元格颜色。
代码如下:
Private Sub Cell_Color_Change()
For Each cell In Range("U2:U19004")
If cell.Value <> 2.04 Or 3.59 Then cell.Interior.ColorIndex = 3
Next cell
End Sub
由于某些原因,代码将整列变为红色。
我试过使用条件格式,同样的事情发生了。
请帮忙。谢谢!
条件格式将执行此操作:
一种。使用公式:AND(U2<>2.04,U2<>3.59)
b.选择您的填充颜色
C。应用到 U2:U19004
但是如果你想对其进行编码,if 应该是:
If cell.Value <> 2.04 AND cell.Value <> 3.59 Then cell.Interior.ColorIndex = 3
更正您的 AND
和 OR
:
Private Sub Cell_Color_Change()
For Each cell In Range("U2:U19004")
If cell.Value <> 2.04 And cell.Value <> 3.59 Then cell.Interior.ColorIndex = 3
Next cell
End Sub
编辑#1:
要查找四舍五入到两位小数的值,请尝试以下替代方法:
Private Sub Cell_Color_Change()
Dim cv As Variant
For Each cell In Range("U2:U19004")
cv = Application.WorksheetFunction.Round(cell.Value, 2)
If cv <> 2.04 And cv <> 3.59 Then cell.Interior.ColorIndex = 3
Next cell
End Sub
使用 VBA 设置条件格式。
Option Explicit
Private Sub set_Cell_Color_Change()
With Range("U2:U19004")
.FormatConditions.Delete
.FormatConditions.Add Type:=xlExpression, Formula1:="=and(round($u2, 2)<>2.04, round($u2, 2)<>3.59)"
.FormatConditions(.FormatConditions.Count).Font.Color = vbRed
End With
End Sub
我正在尝试获取 U 列中不等于 2.04 或 3.59 的所有单元格以更改单元格颜色。
代码如下:
Private Sub Cell_Color_Change()
For Each cell In Range("U2:U19004")
If cell.Value <> 2.04 Or 3.59 Then cell.Interior.ColorIndex = 3
Next cell
End Sub
由于某些原因,代码将整列变为红色。 我试过使用条件格式,同样的事情发生了。 请帮忙。谢谢!
条件格式将执行此操作:
一种。使用公式:AND(U2<>2.04,U2<>3.59)
b.选择您的填充颜色
C。应用到U2:U19004
但是如果你想对其进行编码,if 应该是:
If cell.Value <> 2.04 AND cell.Value <> 3.59 Then cell.Interior.ColorIndex = 3
更正您的 AND
和 OR
:
Private Sub Cell_Color_Change()
For Each cell In Range("U2:U19004")
If cell.Value <> 2.04 And cell.Value <> 3.59 Then cell.Interior.ColorIndex = 3
Next cell
End Sub
编辑#1:
要查找四舍五入到两位小数的值,请尝试以下替代方法:
Private Sub Cell_Color_Change()
Dim cv As Variant
For Each cell In Range("U2:U19004")
cv = Application.WorksheetFunction.Round(cell.Value, 2)
If cv <> 2.04 And cv <> 3.59 Then cell.Interior.ColorIndex = 3
Next cell
End Sub
使用 VBA 设置条件格式。
Option Explicit
Private Sub set_Cell_Color_Change()
With Range("U2:U19004")
.FormatConditions.Delete
.FormatConditions.Add Type:=xlExpression, Formula1:="=and(round($u2, 2)<>2.04, round($u2, 2)<>3.59)"
.FormatConditions(.FormatConditions.Count).Font.Color = vbRed
End With
End Sub