简体   繁体   中英

Adding a Sumif formula to cell error using c#

I'm trying to add a formula to a cell in multiple rows in Excel through C#. What i'm trying to achieve with the formula is to SUM all the cells which does not contain the string N/A , in column D to S in every row.

This is the formula i'm trying to add:

=SUMIF(DX:SX,"<>N/A")

(In the example above i changed the actual row number with X)

So i'm looping through the rows and doing this:

    foreach (var row in rows)
    {
        ws.Cells[row, 2].Formula = string.Format("=SUMIF(D{0}:S{0},\"<>N/A\")", row + 1);                          
    }

Here I get the exception:

System.ArgumentException : Failed to parse: =SUMIF(D2:S2,"<>N/A"). Error: Unsupported function: SUMIF.For list of supported functions consult GemBox.Spreadsheet documentation.

I've also tried to add the Swedish version of the formula which is:

=SUMMA.OM(DX:SX;"<>N/A")

(In the example above i changed the actual row number with X)

and here i get the exception:

System.ArgumentException : Failed to parse: =SUMMA.OM(D2:S2;"<>N/A"). Error: Not expected: SUMMA

The weirdest part of this is that if i manually enter the swedish formula in the cell in Excel, it works.

Appreciate any help i can get, thank you so much.

I actually don't see any reason it would throw that error -- what you did seems like it should work. In the spirit of offering another avenue to test, you could also try the R1C1 version of the formula and see if that works:

foreach (var row in rows)
{
    ws.Cells[row, 2].FormulaR1C1 = "=SUMIF(R[0]C[2]:R[0]C[17],\"<>N/A\")";
}

Unless your rows variable contains an invalid number, I'm not sure why it would throw that exception. In the example error you gave, that should be fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM