2

I want to write this code in C#:

//
excelCellrange = excelSheet.get_Range("B1", "B1");
excelCellrange.Formula = "=IF(A1="Harm",100)";

//Copy formula to other cells:
excelCellrange = excelSheet.get_Range("B2", B10);
excelCellrange.PasteSpecial(Excel.XlPasteType.xlPasteFormulas);

But I cant write "Harm" in "". How to solve this?

user3812553
  • 129
  • 2
  • 9
  • 1
    Try: `excelCellrange.Formula = "=IF(A1=\"Harm\",100)"; ` – Abbas Jul 07 '14 at 13:37
  • possible duplicate: http://stackoverflow.com/questions/1928909/in-c-can-i-escape-a-double-quote-in-a-verbatim-string-literal –  Jul 07 '14 at 14:21

2 Answers2

6

You need to escape your quotation marks like this:

excelCellrange.Formula = "=IF(A1=\"Harm\",100)";

You can find a good list of escape sequences here: http://blogs.msdn.com/b/csharpfaq/archive/2004/03/12/what-character-escape-sequences-are-available.aspx

DavidG
  • 113,891
  • 12
  • 217
  • 223
0

You need to escape the quotes by preceding them with a \ so the line would become excelCellrange.Formula = "=IF(A1=\"Harm\",100)";

petelids
  • 12,305
  • 3
  • 47
  • 57