Share via

Microsoft excel Evaluate function returns a code error (-2146826273)

Yamen Jeribi 41 Reputation points
2021-05-24T11:12:03.387+00:00

Hello,

I'm using microsoft.office.interop.excel dll in my application .
when I execute the evaluation function with the below formula : =IF(100000>15000,20%,IF(100000>12500,17.5%,IF(100000>10000,15%,IF(100000>7500,12.5%,IF(100000>5000,10%,0)))))*IF((AND(40>=20,23>=30)),15,2000)*LOG(IF(AND(3<>0,(1/3)>0.5),1000,2000))/IF(90<>0, IF((1/90)>0.5, 123456, 987654), 987654)-550-IF((OR(25>=20, 1000>=30)), 5000, 900) .
I get a code error -2146826273. I test the formula using excel sheet and it worked perfectly. Please find below the executed code.

public dynamic EvaluteFormula(string expressionFormula)
    {
        Application excelApp = new Application();
        return excelApp.Evaluate(expressionFormula);
    }
    
    

When I minimize the formula length by removing operators, it works perfectly. Has anyone ideas how to solve this problem ?

Thanks!

Microsoft 365 and Office | Excel | For business | Windows
Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

0 comments No comments

Answer accepted by question author
  1. Viorel 126.8K Reputation points
    2021-05-24T12:02:03.14+00:00

    According to documentation, the length is limited to 255 characters. You can try using some worksheet and cell. For example:

    var excelApp = new Application( );
    var wb = excelApp.Workbooks.Add( Type.Missing );
    var ws = wb.Worksheets[1];
    var r = ws.Range["A1"];
    r.Formula = expressionFormula;
    var result = r.Value;
    

    The result is -5549.9986630824169.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.