title: Find the last used row in Excel with C#
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/1284388/how-to-get-the-range-of-occupied-cells-in-excel-sheet">How to get the range of occupied cells in excel sheet</a>  </p>
  </blockquote>
  
  
  
  <p>Im trying to find the last used row in an Excel worksheet. For doing this, I'm using this code:     </p>
  
  <pre><code>int lastUsedRow = currentWS.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell,Type.Missing).Row;
  </code></pre>
  
  <p>Most of the time it works fine, but sometimes Excel thinks that there's more rows in the sheet than theres suppose to be.</p>
  
  <p>Fx: If I copy data from sheet1, containing 12 rows, to an empty sheet2, then deleting all data in sheet2, by right clicking and press "Delete..." and copy data from sheet3, containing 5 rows, to sheet2, then lastUsedRow will give me the value of 12 which should have been 5.</p>
  
  <p><img src="https://i.stack.imgur.com/9TQQy.jpg" alt="enter image description here">
  The image example above is suppose to give my the value of 22 as row count, but instead i'll get 634. Notice the scroll bar to the right.</p>
  
  <p>It seems like Excel thinks that some cells are filled even though I just deleted all cells, before copying new data with fewer rows into the sheet.</p>
  
  <p>Are there any way to "resize" the view of the data in the sheet, so that i'll get the right number of used cells or maybe another way to find the number of the last used row?</p>
  
  <p>Thanks.</p>