1

I have this VBA code for conditional formatting.

Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlNotBetween, _
    Formula1:="=$I$10", Formula2:="=$J$10"
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

I have a vbscript that creates an Excel sheet and I need to apply that VBA code to a cell in the Excel sheet as it is created. Having issues getting it to run. I know I need to sub the actual values for the excel constants but there's more to it I just don't get

What I've done so far

     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

I need it to apply the conditional formatting to the a specific cell (the one I defined as priceRange)

niton
  • 8,771
  • 21
  • 32
  • 52
asdfasfd
  • 281
  • 1
  • 6
  • 19
  • Can you post more of the code? I see you're using "Selection" a lot, which can cause errors (you want to [avoid](http://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba-macros) using "select" whenever possible). – BruceWayne Apr 22 '15 at 19:33
  • @user3578951 That's the vba macro excel generated. Instead of selection I need to make it apply to a specific row since the excel object is hidden until it's finished the script. I'll edit what I did but I know it's wrong. I did get the constants in but I'm not sure where to go from there. – asdfasfd Apr 22 '15 at 19:36
  • You'll get more help if you post code for a minimal, complete, verifiable example (http://stackoverflow.com/help/mcve) and explain what result you expect, and what result you obtain. – Joe Apr 22 '15 at 19:52

1 Answers1

2

Untested:

Dim rng, fc, rowNum, finalRowNum, objWorkSheet

'...
'...
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
Tim Williams
  • 154,628
  • 8
  • 97
  • 125