1

I'm trying to add conditional formatting to my VSTO Excel C# Application. I want to set the cell background colour to red if the column value in AH is the same as the column value in X and if X is not blank. However it complains at the below code saying

Syntax error ',' expected.

This formula works fine when I manually put it in excel.

This is my formula: =AND(X7=AH7,X7<>"")

And here's my C# code:

FormatCondition format7 =
    (FormatCondition)(uiWorksheet.get_Range("AH7:AH" + lastUsedRow, Type.Missing)
        .FormatConditions.Add(XlFormatConditionType.xlExpression,
            XlFormatConditionOperator.xlEqual, "=AND(X7=AH7,X7<>"")",
           Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing));

format7.Interior.Color = System.Drawing.Color.Red;
roa765
  • 285
  • 3
  • 19
  • 2
    You need to escape those `"` in the formula: `"=AND(X7=AH7,X7<>\"\")"` – rene Dec 31 '19 at 10:34
  • 3
    the Char `"` is a string delimiter. If you want to have `"` in your string you have to escape it. [How to add double quotes to a string?](https://stackoverflow.com/questions/3905946/) – xdtTransform Dec 31 '19 at 10:38
  • Did that solve your issue? @xdtTransform- would you be open to posting that solution as an answer? – Colm Bhandal May 22 '20 at 16:53

0 Answers0