0

I Have a C# Project, where I need to change Values in Excel Cells. This works on some parts and on other not, which is really wierd. The Part which does not work is here:

    exApp =  new Microsoft.Office.Interop.Excel.Application();         
        Excel.Workbook exWb = exApp.Workbooks.Open(filepath);
        Excel.Worksheet SumSheet = exWb.Worksheets[1];

        Excel.Range line = (Excel.Range)SumSheet.Rows[4];
        line.Insert();
        SumSheet.Range[SumSheet.Cells[13, 4], SumSheet.Cells[13, 4]].Value = "t1";
        Debug.Print((string)SumSheet.Range[SumSheet.Cells[13, 4], SumSheet.Cells[13, 4]].Value);
        SumSheet.Range[SumSheet.Cells[15, 4], SumSheet.Cells[15, 4]].Value = "t2";
        SumSheet.Range[SumSheet.Cells[15, 4], SumSheet.Cells[15, 5]].FormulaR1C1 = "=SUMME(O6:O400)";

It gives me the error when i try to change the value. The following Code works though:

 for (int i = 2; i <= exWb.Worksheets.Count; i++)
        {
            int counter = 0;
            int counterMax = 0;
            Excel.Worksheet exWs = exWb.Worksheets[i];
           line = (Excel.Range)exWs.Rows[15];
            line.NumberFormat = "##";
            line.Insert();               
            line.Insert();
            line.Insert();

            for (int j = 2 ; j <= exWs.UsedRange.Columns.Count; j++)
            {                   
                if (exWs.Range[exWs.Cells[13, j], exWs.Cells[13, j]].Value <= 0.3)
                {
                    counter ++;
                    exWs.Range[exWs.Cells[14, j], exWs.Cells[14, j]].Value = 1;
                    exWs.Range[exWs.Cells[14, j], exWs.Cells[14, j]].Interior.Color = Color.Green;
                    exWs.Range[exWs.Cells[15, j], exWs.Cells[15, j]].Value = counter;
                    exWs.Range[exWs.Cells[15, j], exWs.Cells[15, j]].Interior.Color = Color.Green;
                    counterMax = (counter > counterMax) ?  counter : counterMax;
                }
                else
                {
                    counter = 0;
                }                    
            }
            exWs.Range[exWs.Cells[16, 2], exWs.Cells[16, 2]].Value = counterMax;
            fillStart(exWs.Name, counterMax, SumSheet);

            if (counterMax > 45)
            {
                exWs.Range[exWs.Cells[16, 2], exWs.Cells[16, 2]].Interior.Color = Color.Green;
            }
            else
            {
                exWs.Range[exWs.Cells[16, 2], exWs.Cells[16, 2]].Interior.Color = Color.Red;
            }
        }

Any ideas?

Eych
  • 313
  • 1
  • 5
  • 14
  • What is *"the error"* you're getting? Where exactly do you get that error? If it is an exception please include all relevant information: indicate the line which thrown the exception, include the full exception message and also the stack strace of the exception. – bassfader Jun 20 '17 at 08:56
  • "Internal error" is a pretty meaningless error message with no diagnostic power. That happens. Nothing you can do but tinker with the code until the error disappears, whatever you changed was the cause of the error. – Hans Passant Jun 20 '17 at 10:13
  • The Error which I get is "Internal error in the expression evaluator". I get it in the upper part when i try to change a value, e.g.: SumSheet.Range[SumSheet.Cells[13, 4], SumSheet.Cells[13, 4]].Value = "t1"; – Eych Jun 20 '17 at 11:40
  • I Found the Answer in this Post: https://stackoverflow.com/questions/21854426/get-internal-error-in-the-expression-evaluator-on-add-watch-function-when-tr?rq=1 – Eych Jun 22 '17 at 08:12

0 Answers0