Here is my C# code to write ArrayFormula to certain cell in excel.
I am using UFT (Unified Functional Testing) which uses C# for custom code.
String sheetName = "xyz";
String wsMethodName = "abc";
int i = 2;
Excel.Application xlApp = null;
xlApp = new Excel.ApplicationClass();
wb = xlApp.Workbooks.Open(srcFile,
0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",
true, false, 0, true, false, false);
worksheet = (Excel.Worksheet)wb.Worksheets[sheetName];
Excel.Range excelCell = (Excel.Range)worksheet.get_Range("B2", "B21");
foreach (Excel.Range c in excelCell)
{
//
strAvgFormula =
"=AVERAGEIFS(" +
"(OFFSET(\'" + sheetName + "\'!$A$1,2,2,COUNTA(\'" + sheetName + "\'!$A:$A)-2,1))," +
"OFFSET(\'" + sheetName + "\'!$A$1,2,16382,COUNTA(\'" + sheetName + "\'!$A:$A)-2,1)," +
"(MID(C" + i + ",1,(FIND(\"-\",C" + i + "))-2))," +
"OFFSET(\'" + sheetName + "\'!$A$1,2,16383,COUNTA(\'" + sheetName + "\'!$A:$A)-2,1)," +
"(MID(C" + i + ",(FIND(\"-\",C" + i + ")+1),(FIND(\"/\",C" + i + "))-(FIND(\"-\",C" + i + ")+1))))";
this.CodeActivity16.Report("strAvgFormula",strAvgFormula);
//
strMaxFormula =
"=MAX(" +
"IF((OFFSET(\'" + sheetName + "\'!$A$1,2,16382,COUNTA(\'" + sheetName + "\'!$A:$A)-2,1)=MID(C" + i + ",1,(FIND(\"-\",C" + i + "))-2))*" +
"(OFFSET(\'" + sheetName + "\'!$A$1,2,16383,COUNTA(\'" + sheetName + "\'!$A:$A)-2,1)=MID(C" + i + ",(FIND(\"-\",C" + i + ")+2)," +
"(FIND(\"/\",C" + i + "))-(FIND(\"-\",C" + i + ")+2)))," +
"OFFSET(\'" + sheetName + "\'!$A$1,2,2,COUNTA(\'" + sheetName + "\'!$A:$A)-2,1)))";
this.CodeActivity16.Report("strMaxFormula",strMaxFormula);
if (c.Value2.ToString() == wsMethodName)
{
newExcelCell = (Excel.Range)worksheet.get_Range("F" + i, "F" + i);
newExcelCell.Clear();
newExcelCell.FormulaArray = strAvgFormula; //Failing @ this line, error is mentioned below
//newExcelCell.Value = strAvgFormula;
newExcelCell = (Excel.Range)worksheet.get_Range("G" + i, "G" + i);
newExcelCell.Clear();
newExcelCell.FormulaArray = strMaxFormula;
//newExcelCell.Value = strMaxFormula;
break;
}
i ++;
}
wb.Save();
xlApp.Workbooks.Close();
xlApp.Quit();
releaseObject(newExcelCell);
releaseObject(excelCell);
releaseObject(worksheet);
releaseObject(wb);
releaseObject(xlApp);
private void releaseObject(object obj)
{
try
{
Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
CodeActivity16.Report("Error","Unable to release the Object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
Now, If I copy the same formula from printed output result and paste it in my desired cell, it is working fine.
Escape characters are doing their job properly.
If I change newExcelCell.FormulaArray to newExcelCell.Value, than it is writing to excel but it works as normal formula and not the ArrayFormula (like we do Ctrl + Shift + Enter).
Here is the error that I am getting from result file:
The formula you typed contains an error. Try one of the following:
• Make sure you've included all parentheses and required arguments.
• To get assistance with using a function, click Function Wizard on the Formulas tab (in the Function Library group).
• If you include a reference to another sheet or workbook, verify that the reference is correct.
• If you are not trying to enter a formula, avoid using an equal sign (=) or minus sign (-), or precede it with a single quotation mark ( ' ).
• For more information about common formula problems, click Help.
Thanks in advance for any help or suggestion.
UPDATE:
Here is the formula for strAvgFormula that I am trying to write.
"=AVERAGEIFS(" +
"(OFFSET('1'!$A$1,2,2,COUNTA('1'!$A:$A)-2,1))," +
"OFFSET('1'!$A$1,2,16382,COUNTA('1'!$A:$A)-2,1),(MID(C2,1,(FIND("-",C2))-2))," +
"OFFSET('1'!$A$1,2,16383,COUNTA('1'!$A:$A)-2,1),(MID(C2,(FIND("-",C2)+1),(FIND("/",C2))-(FIND("-",C2)+1))))"
Formula for strMaxFormula is working fine.
So, finally I got the workaround for this issue.
It turned out (this is what I feel) that either the Excel is not accepting the formula from the string strAvgFormula or it get messed up because of cell reference using Offset.
I've to use direct cell reference.
Here is my working formula:
int lastUsedRowDiffSheet = worksheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell,Type.Missing).Row;
strAvgFormula =
"=AVERAGEIFS('" + sheetName + "\'!C" + iRowCount + ":C" + lastUsedRowDiffSheet + ","
+ "'" + sheetName + "'!XFC" + iRowCount + ":XFC" + lastUsedRowDiffSheet + ","
+ "MID(C" + i + ",1,(FIND(\"-\",C" + i + "))-2),"
+ "'" + sheetName + "\'!XFD" + iRowCount + ":XFD" + lastUsedRowDiffSheet + ","
+ "(MID(C" + i + ",(FIND(\"-\",C" + i + ")+1),(FIND(\"/\",C" + i + "))-(FIND(\"-\",C" + i + ")+1))))";