c# excel range.FormulaArray, if you have time can you try below c# program with two different FormulaArrays

StackOverflow https://stackoverflow.com/questions/1045819

  •  20-08-2019
  •  | 
  •  

Question

can anyone tell me why this does not work

 Excel.Worksheet ws_res = (Excel.Worksheet)
                  wb.Worksheets.Add(mis, mis, mis, mis);
  Excel.Range range_res = (Excel.Range)ws_res.get_Range("A1","HM232");
 range_res.FormulaArray = 
           "=ROUND((IF((IF(Sheet4!A1:HM232=1,0,"+
           "IF(Sheet4!A1:HM232=0,1,Sheet4!A1:HM232)))=1,0,"+
           "IF((IF(Sheet4!A1:HM232=1,0,"+
           "IF(Sheet4!A1:HM232=0,1,Sheet4!A1:HM232)))=0,1,("+
           "IF(Sheet4!A1:HM232=1,0,"+
           "IF(Sheet4!A1:HM232=0,1,Sheet4!A1:HM232)))))),0)";

but this does

   Excel.Worksheet ws_res = (Excel.Worksheet)
                  wb.Worksheets.Add(mis, mis, mis, mis);
  Excel.Range range_res = (Excel.Range)ws_res.get_Range("A1","HM232");
  range_res.FormulaArray = 
  "=ROUND((IF(Sheet4!A1:HM232=1,0,IF(Sheet4!A1:HM232=0,1,Sheet4!A1:HM232))),0)";

=======================================================

but if i copy and paste the above two formulas to the formula bar of excel-2007 and press Ctrl+Shift+Enter, both work perfectly!

=======================================================

i have also changed in the above two formulas A1:HM232 to R1C1:R232C221, again from c# the shorter one works fine, but the longer one still generates the exception!

Was it helpful?

Solution

It works for me. I have to assume either that you don't have a Sheet4 (unlikely, since the shorter formula works) or that you made a typo when removing the Quotes, "+"s, and line breaks. Here is what I have in my formula bar now:

=ROUND((IF((IF(Sheet4!A1:HM232=1,0,IF(Sheet4!A1:HM232=0,1,Sheet4!A1:HM232)))=1,0,IF((IF(Sheet4!A1:HM232=1,0,IF(Sheet4!A1:HM232=0,1,Sheet4!A1:HM232)))=0,1,(IF(Sheet4!A1:HM232=1,0,IF(Sheet4!A1:HM232=0,1,Sheet4!A1:HM232)))))),0)

OTHER TIPS

Because of Excel has some problem to execute FormulaArray if the string is more than 255 chars. Annoying. VBA has a round way, C#, No way!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top