Reputation: 389
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
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
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
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
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