使用Excel VB来更改从Access导入的数据颜色编码相应

我想要做的是改变特定的范围单元格为特定的颜色。 它是有效的,当有一个增加,当它是-2.00%,应该是红色的。 但是,从前一次减less,应该是绿色的,一旦低于-2.00%,它应该再次变黑。

所以基本上数据的单元格从C2开始,到H54结束。 它是以C2为主的行格式工作,D2是继续数据等.C3是新的主数据,D3是数据的继续等。

我的代码,我一直在testing,但没有得到正确的如下:

Range("C2").Select If Range("C2").Value >= "-2.00%" Then With Selection.Font .Color = -16776961 .TintAndShade = 0 End With ElseIf Range("C2").Value < "-2.00%" Then With Selection.Font .ThemeColor = xlThemeColorLight1 .TintAndShade = 0 End With End If Range("D2").Select If Range("D2").Value <= "-2.00%" & Range("C2").Value Then With Selection.Font .Color = -11489280 .TintAndShade = 0 End With ElseIf Range("D2").Value > "-2.00%" & Range("C2").Value Then With Selection.Font .Color = -16776961 .TintAndShade = 0 End With ElseIf Range("D2").Value < "-2.00%" Then With Selection.Font .ThemeColor = xlThemeColorLight1 .TintAndShade = 0 End With End If Range("E2").Select If Range("E2").Value <= "-2.00%" & Range("D2").Value Then With Selection.Font .Color = -11489280 .TintAndShade = 0 End With ElseIf Range("E2").Value > "-2.00%" & Range("D2").Value Then With Selection.Font .Color = -16776961 .TintAndShade = 0 End With ElseIf Range("E2").Value < "-2.00%" Then With Selection.Font .ThemeColor = xlThemeColorLight1 .TintAndShade = 0 End With End If 

但是,当它低于2.00%时,它仍然是绿色的,并且即使再次增加,同样的错误仍然存​​在。

我将不胜感激任何协助,以尽快完成这项工作…如果你知道一个较短的方法,请把它放下来,我来testing它。 非常感谢您抽出时间来回顾一下。

以下是结果的示例图片以及实际应该是什么: 在这里输入图像说明

这似乎遵循您的业务逻辑,因为我从代码和示例图像中感受到它。

 Sub ject() Dim r As Long, c As Long, vRTRNs As Variant, thrshld As Double thrshld = 0.02 With Worksheets("Sheet2") With .Cells(1, 1).CurrentRegion With .Resize(.Rows.Count - 1, .Columns.Count - 2).Offset(1, 2) .Cells.Font.ColorIndex = xlColorIndexAutomatic vRTRNs = .Value2 For r = LBound(vRTRNs, 1) To UBound(vRTRNs, 1) 'deal with the first value If vRTRNs(r, LBound(vRTRNs, 2)) >= thrshld Then .Cells(r, 1).Font.Color = vbRed End If 'the remainder of the columns in the row For c = LBound(vRTRNs, 2) + 1 To UBound(vRTRNs, 2) Select Case vRTRNs(r, c) Case Is >= thrshld .Cells(r, c).Font.Color = _ IIf(vRTRNs(r, c) >= vRTRNs(r, c - 1), vbRed, vbGreen) Case Is < thrshld .Cells(r, c).Font.ColorIndex = xlColorIndexAutomatic End Select Next c Next r End With End With End With End Sub 

结果:

returns_business_logic