如果单元格不在VBA之间的VBA条件格式

时间:2015-04-22 16:31:55

标签: vba excel-vba vbscript excel

我需要使用VBScript将条件格式应用于单元格,以便在单元格的值不在其他两个单元格的值之间时更改单元格的背景颜色:

Picture of an Excel Dialog

来自宏的代码

Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlNotBetween, _
    Formula1:="=$I$28", Formula2:="=$J$28"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
    .PatternColorIndex = xlAutomatic
    .Color = 255
    .TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
End Sub

我转换为无效的vbscript

    priceRange = "K"&rowNum + 2
    objWorkSheet.Range(priceRange).FormatConditions.Add Type:=1, Operator:=2, Formula1:="=$I$"&finalRowNum + 1&"", Formula2:="=$J$"&finalRowNum + 1&""
    objWorkSheet.Range(priceRange).FormatConditions(objExcel.Selection.FormatConditions.Count).SetFirstPriority
    objWorkSheet.Range(priceRange).FormatConditions(1).Interior.PatternColorIndex = -4105
    objWorkSheet.Range(priceRange).FormatConditions(1).Interior.Color = 255
    objWorkSheet.Range(priceRange).FormatConditions(1).Interior.TintAndShade = 0
    objWorkSheet.Range(priceRange).FormatConditions(1).StopIfTrue = False

1 个答案:

答案 0 :(得分:1)

有效的代码

Set rng = objWorkSheet.Range("K" & rowNum + 2)

'vbscript doesn't support named arguments, only positional
Set fc = rng.FormatConditions.Add(1, 2, _
                                 "=$I$" & finalRowNum, _
                                 "=$J$" & finalRowNum)
fc.SetFirstPriority

With fc.Interior
    .PatternColorIndex = -4105
    .Color = 255
    .TintAndShade = 0
End With

fc.StopIfTrue = False