如何在vba中表示两个单元格的差异值?

时间:2014-08-28 20:16:02

标签: excel vba excel-vba

我的目标是进行条件格式化。对于范围B2:B21,我需要突出显示不在值D2-C2(平均减去标准差)和D2 + C2(平均值加标准开发)之间的所有值。我无法让它工作,所以我记录下来了。但不是硬编码公式1和公式2中的值(28,40),而是想要D2-C2和D2 + C2的值。有任何线索如何做到这一点?

Sub Conditionalformatting()

Range("B2:B21").Select
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlNotBetween, _
    Formula1:="=28", Formula2:="=40"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
    .PatternColorIndex = xlAutomatic
    .ColorIndex = xlAutomatic
End With
Selection.FormatConditions(1).StopIfTrue = False
End Sub 

enter image description here

2 个答案:

答案 0 :(得分:1)

formula1=cells(2,4).value-cells(2,3)
formula2=cells(2,4).value+cells(2,3)

这应该可以胜任。

答案 1 :(得分:1)

Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlNotBetween, _
    Formula1:="=$D$2-$C$2", Formula2:="=$D$2+$C$2"

完成宏

Sub ColorMe()
Selection.FormatConditions.Delete
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlNotBetween, _
    Formula1:="=$D$2-$C$2", Formula2:="=$D$2+$C$2"

    With Selection.FormatConditions(1).Interior
        .PatternColorIndex = xlAutomatic
        .Color = 13551615
        .TintAndShade = 0
    End With
Selection.FormatConditions(Selection.FormatConditions.Count).StopIfTrue = False

End Sub