I’m trying to color a spreadsheet based on the results given in one of it’s columns. I’m using the following code:
With newSheet.Range("B:B")
.FormatConditions.Add(Excel.XlFormatConditionType.xlCellValue, Excel.XlFormatConditionOperator.xlEqual, "CORRECT")
.FormatConditions(1).Interior.ColorIndex = 4
.FormatConditions.Add(Excel.XlFormatConditionType.xlCellValue, Excel.XlFormatConditionOperator.xlEqual, "INCORRECT")
.FormatConditions(2).Interior.ColorIndex = 3
End With
Unfortunately this only colors the cell containing “CORRECT” or “INCORRECT”. I’d like it to extend to the row they are in (for example, if B12 contains “CORRECT”, I want A12:G12 to all be coloured green). It was suggested that I try using an expression and so I tried the following code:
.FormatConditions.Add(Type:=XlFormatConditionType.xlExpression, Formula1:="=B" & row & "= ""CORRECT"")")
.FormatConditions(1).Interior.ColorIndex = 4
This however, returns an E_INVALIDARG exception. I’d appreciate any tips on how to go about fixing this. I should also note that looping through every row and checking one at a time is not really an option, as there are many thousands of lines.
Your formula should work once you remove your excess closing parenthesis and make the column an absolute value
Make sure you set the row in your formula
$B1as the first row of your formatted range (you don’t need to do a loop)