VBA Conditional Formatting if Cell Not Between

匿名 (未验证) 提交于 2019-12-03 02:29:01

问题:

I need to apply conditional formatting to a cell using VBScript to change the background color of the cell if it's value is not between the value of two other cells:

code from the macro

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 

My conversion to vbscript that isn't working

    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:

The code that worked

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 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!