3
votes

Info: The following code is in a foreach loop

Works:

indexSheet.Cell("G" + cellIndex).FormulaA1 = "=SUM(A1:A10)";

Only works, when i type this directly in excel:

indexSheet.Cell("G" + cellIndex).FormulaA1 = "=FIND(\"erw\";H5)"; // with Property
indexSheet.Cell("G" + cellIndex).SetFormulaA1("=FIND(\"erw\";H5)");  //with Function
indexSheet.Cell("G" + cellIndex).FormulaA1 = $"COUNTIF(H{cellIndex};\"*\"&$C$2&\"*\")*(MAX($G$4:G{cellIndex - 1})+1)";
indexSheet.Cell("B" + cellIndex).FormulaA1 = $"=IF(ROW()-{cellIndex}>MAX(G:G);\"\";HYPERLINK(CONCATENATE(\"#\";(INDEX(H:H;MATCH(ROW()-4;G:G;0)));\"!B{cellIndex}\");(INDEX(H:H;MATCH(ROW()-4;G:G;0)))))";

I've checked/tried:

  1. Because in stackoverflow exists a similar case: If the language of my functions matching with my excel language (both are english)
  2. I tried if simpler formulas would work like SUM() or FIND() --> (-SUM works, FIND doesn't work )
  3. I typed the formulas directly in an excelsheet (formulas working here)
  4. I tried it outside from the Loop with a hard-coded cell-informations and without the "cellindex"-variable
  5. Checked out the closedXML-documentation
  6. Whit equal signs at the beginning of the formulas and without it
  7. I tried with closedXML Vers. 0.76.0 and 0.80.1 (latest stable)

Excel shows me this: Picture1

And this: Picture2

The Linked XML-logfile from Picture 2 contains informations about the deleted parts

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<recoveryLog xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
    <logFileName>error035720_03.xml</logFileName><summary>Errors were detected in file 'C:\Users\XXX\Documents\Output\CodeBook.xlsx'</summary>
    <removedRecords>
        <removedRecord>Removed Records: Formula from /xl/worksheets/sheet.xml part</removedRecord>
        <removedRecord>Removed Records: Formula from /xl/calcChain.xml part (Calculation properties)</removedRecord>
    </removedRecords>
</recoveryLog>
1

1 Answers

5
votes

you can't use semicolon (;) to separate arguments in closedxml. Use comma(,) like this:

indexSheet.Cell("G" + cellIndex).FormulaA1 = "=FIND(\"erw\",H5)";

that is the reason why SUM(range) works but FIND(find_text;within_text) does not...