title: Select, Copy, and Re-Insert a range of Excel Rows Using C#
body: <p>Using C# to replace an existing and now defunct legacy routine in Delphi, I’ve been trying re-develop a routine in C# that worked well in Delphi 7 that will open each of my 30 Excel files, select the last 22 rows in each, copy them, and reinsert them back into their respective files. </p>
  
  <p>For example, I find the last row, August of 2019 has 22 business days so I begin my range 22 rows back from the last row.</p>
  
  <p>Using the Excel file, I want to select the rows that range from <code>A4157</code> to <code>A4178</code>.  Then, I want to take the rows I’ve copied and insert them after row <code>A4156</code> or before row <code>A4157</code>. I then take a separate list of August dates and write them to each of the lower 22 from <code>A4178</code> to <code>A4200</code>. July is then complete and rows for August are ready to go.</p>
  
  <p>I retain the last row because additional calculations on other worksheets in the same workbook refer to the last row for data and they will index automatically from <code>A4178</code> to <code>A4200</code> doing it this way.</p>
  
  <p>As each entry made during the month, its data are copied down to the last row so the last row is always up to date.  Another sheet in the workbook uses up-to-date data in its summary.</p>
  
  <p>So far, I can open the Excel workbook and get the right sheet.</p>
  
  <p>The following does highlight the proper row but I get a runtime error. I’m not sure what that means.  </p>
  
  <p>I’ve commented out the “select” line and run the “copy” line with the same result.  I’m now into my second week trying to work out this problem.  I need an example of a functioning routine if possible.  A link to a text on C# - MS interfacing would be a great help.</p>
  
  <pre><code>Excel.Range selectRange = excelWorkSheet.Range[excelWorkSheet.Cells[A4157], excelWorkSheet.Cells[A4178].select;
  Excel.Range copyRange = excelWorkSheet.Range[excelWorkSheet.Cells[A4157], excelWorkSheet.Cells[A4178]].select;
  </code></pre>
  
  <blockquote>
    <p>Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'Cannot implicitly convert type 'bool' to 'Microsoft.Office.Interop.Excel.Range''</p>
  </blockquote>