user3812553
user3812553

Reputation: 129

How to escape double quotation marks in a formula

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?

Upvotes: 2

Views: 1457

Answers (2)

petelids
petelids

Reputation: 12815

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

Upvotes: 0

DavidG
DavidG

Reputation: 118947

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

Upvotes: 6

Related Questions