wouter
wouter

Reputation: 389

putting this line into excel.Formula "=IF(C4 = "x";1;0)" isnt working because of double `" "`

I want to put this line into a excel cell with c# : =IF(C4 = "x";1;0)

But when I type: .Formula = "=IF(C4 = "x";1;0)", it cuts the sentence into 2 seperate parts, because of the double ", any idea how to fix this?

Thank in advance

Upvotes: 1

Views: 127

Answers (5)

Solar Mike
Solar Mike

Reputation: 8375

Just to note, excel uses either commas or semicolons depending on the system language settings of the computer it is installed on.

Upvotes: 0

Luke Cummings
Luke Cummings

Reputation: 367

You could escape each individual character, using the escape character \:

range.Forumula = "=IF(C4 = \"x\",1,0);"

Alternatively, you can denote the block as a string literal by adding the @ character before your string:

range.Formula = @"=IF(C4 = "x",1,0)";

Just be careful with using string literals, as they will disable the escaping of characters all together:

Console.WriteLine("Hello,\tWorld"); //prints: Hello, World

Console.WriteLine(@"Hello,\tWorld"); //prints: Hello,\tWorld

More information on string literals: https://msdn.microsoft.com/en-us/library/aa691090(v=vs.71).aspx

One more note, Excel uses , to separate parameters in formulas, not ; as you have in your example.

Upvotes: 1

tbm0115
tbm0115

Reputation: 419

You can use ASCII character



    'VB.NET:
    "=IF(C4 = " & Chr(34) & "x" & Chr(34) & ",1,0)"

    'C#:
    "=IF(C4 = " + Char.ConvertFromUtf32(34) + "x" + Char.ConvertFromUtf32(34) + ",1,0)";

    'VBA:
    "=IF(C4 = \"x\",1,0)"

ASCII character number 34 is double quote.

Upvotes: 0

Brino
Brino

Reputation: 2502

Excel uses commas to separate parameters, not semicolons.

Escape the quotes with a backslash \.

Change it to this: "=IF(C4=\"x\",1,0)"

Upvotes: 1

laskdjf
laskdjf

Reputation: 1183

use the escape characters \" This will read the " as a string

Upvotes: 3

Related Questions