title: C# To Add Total Row To Last Row Of Excel Worksheet
body: <p>I have this syntax, that does not throw any error, and seems like it is the correct code.  However, the total rows are not added.  What is incorrect in my syntax?</p>
  
  <pre><code>if (xlApp.WorksheetFunction.CountA(WS.Cells) != 0)
  {
      lastRow = WS.Cells.Find("*", WS.Range["A1"], Excel.XlFindLookIn.xlFormulas, Excel.XlLookAt.xlPart, Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlPrevious, false).Row;
  }
  else { lastRow = 1; }
  WS.Range["C" + lastRow + 1].FormulaR1C1 = "=SUM(R[-" + lastRow + "]C:R[-1]C)";
  WS.Range["C" + (lastRow + 1) + ":N" + (lastRow + 1)].FillRight();
  WS.Range["N" + lastRow + 1].FillRight();
  </code></pre>
  
  <p><strong>EDIT</strong><br>
  I also tried using the below <code>Special Cells</code> syntax, and same result, no error, but a total row was not added to any of my worksheets</p>
  
  <pre><code>Excel.Range MyLast =   WS.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
  Excel.Range MyRange = WS.get_Range("A1", MyLast);
  
  int MyLastRow = MyLast.Row;
  int MyLastColumn = MyLast.Column;
  
  WS.Range["A" + MyLastRow + 1].FormulaR1C1 = "Totals:";
  WS.Range["C" + MyLastRow + 1].FormulaR1C1 = "=SUM(R[-" + lastRow + "]C:R[-1]C)";
  WS.Range["C" + (MyLastRow + 1) + ":N" + (lastRow + 1)].FillRight();
  WS.Range["N" + MyLastRow + 1].FillRight();
  </code></pre>