自动更改单元格背景颜色Excel VBA
问题描述:
所以基本上我想要一个可以更改单元格颜色的代码。 例如,如果A1 = 1,B1 = 3%,54%或5%,则将B1的背景颜色更改为绿色。否则,如果B1 = 1%或2%将颜色更改为红色。自动更改单元格背景颜色Excel VBA
这是我到目前为止,我似乎无法弄清楚。任何帮助将不胜感激。 (“A1:B1”)。Formula =“= If(A1 = 1 AND B1 = 3%,Range(”A1:B1“)。Interior.ColorIndex = 4)”
末次
答
我认为你可以弄清楚以下工程的代码,以及如何进行必要的修改
Sub color() Dim ws As Worksheet Dim r As Range, c As Range Set ws = ThisWorkbook.ActiveSheet With ws Set r = Range("A1").CurrentRegion End With For Each c In r If c.Value = 1 And ((c.Offset(, 1) = 0.03) Or (c.Offset(, 1) = 0.54) Or (c.Offset(, 1) = 0.05)) Then Range(c, c.Offset(, 1)).Interior.ColorIndex = 4 ElseIf c.Value = 1 And ((c.Offset(, 1) = 0.01) Or (c.Offset(, 1) = 0.02)) Then Range(c, c.Offset(, 1)).Interior.ColorIndex = 3 End If Next End Sub
当我改变的范围(“L2:L7”)将停止工作,可以请你告诉我你的想法可能是错误的? – sa7
更改设置r =范围(“A1”)。设置当前区域r =范围(“L1”)当前区域 – pascalb
非常感谢。有没有一种方法可以将条件语句从1更改为像c.String =“Good”之类的字符串。 – sa7