Error inserting formula into a cell in Excel via C#? Worksheet?

Asked

Viewed 118 times

0

I have the following code:

newWorksheet.Cells[1, 1] = "Status";
var var1 = newWorksheet.Cells[2, 1] as Excel.Range;
var1.Formula = "=CONT.SE(Planilha1!B2:B7;'Ativo')";
newWorksheet.Cells[3, 1] = "=CONT.SE(Planilha1!B2:B7;'Bloqueado";

The First cell, "Status" is not an error. But from the second, in this line:

var1.Formula = "=CONT.SE(Planilha1!B2:B7;'Ativo')";

makes the mistake:

System.Runtime.InteropServices.COMException: 'Exceção de HRESULT: 0x800A03EC'

Can you help me?

I’m using the library: Microsoft.Office.Interop.Excel;

When you insert it like this:

            newWorksheet.Cells[1, 1] = "Status";
            newWorksheet.Cells[2, 1].Formula = 
                "CONT.SE(Planilha1!B2:B7;\"Ativo\")";

The Text is inserted into the cell. However, I need it as a formula, i.e., the "=" before the text.

2 answers

0

newWorksheet.Cells[3, 1] = "=CONT.SE(Sheet 1! B2:B7;'Blocked";

Missing close parentheses in the formula =CONT.SE().

  • Good note. But the error gives in a row above. srsrsrsrs.

0


I found the solution. Excel reads the formulas in English; However, Excel on the machine is Portuguese; So it was set that it should read in English:

 System.Threading.Thread.CurrentThread.CurrentCulture = 
                 new System.Globalization.CultureInfo("en-US");

Thus, the formula was also inserted in English:

            newWorksheet.Cells[2, 1].Formula = "=COUNTIF(Planilha1!B2:B7,\"Ativo\")";
            newWorksheet.Cells[3, 1].Formula = "=COUNTIF(Planilha1!B2:B7,\"Bloqueado\")";
  • by: https://stackoverflow.com/questions/44033148/excelcom-writing-formula-into-cell-results-in-name

Browser other questions tagged

You are not signed in. Login or sign up in order to post.