title: Excel automation: How do I set the range?
body: <p>The standard method to get the range of an Excel spreadsheet in use is the UsedRange-method. I can copy all the used cells like this:</p>
  
  <pre><code>xlWorkSheet.UsedRange.Copy(misValue);
  </code></pre>
  
  <p>Unfortunately it is not a good method because cells are "activated" if the user write something in one and then delete it again. It can have the unintentional consequense that thousands of empty rows and columns are marked (and printed out in my case).</p>
  
  <p>So I have translated <a href="http://www.vbaexpress.com/kb/getarticle.php?kb_id=82" rel="nofollow">this</a> method to C# to get a more accurate range. On debugging, it gives FirstRow = 1, FirstColumn = 1, LastRow = 600, LastColumn = 2, which is what I wanted using my test worksheet as input.</p>
  
  <p>But I still need to set the real used range and copy it. In VB, it is done like this:</p>
  
  <pre><code>Set RealUsedRange = Range(Cells(FirstRow, FirstColumn), Cells(LastRow, LastColumn))
  </code></pre>
  
  <p>How do I set a range and copy it in C#? I want to replace this line:</p>
  
  <pre><code>xlWorkSheet.UsedRange.Copy(misValue);
  </code></pre>
  
  <p>With the (1,1) to (600,2) range.</p>
  
  <p>I have tried this:</p>
  
  <pre><code>return Excel.Range(xlWorkSheet.Cells[FirstRow, FirstColumn], xlWorkSheet.Cells[LastRow, LastColumn]);
  </code></pre>
  
  <p>But it gives Excel.Range is a 'type', which is not valid in the given context. I don't know the proper way to write it.</p>
  
  <pre><code>    // this struct is just to make the result more readable
      public struct RealRange
      {
          public int FirstRow;
          public int FirstColumn;
          public int LastRow;
          public int LastColumn;
  
          public RealRange(int fr, int fc, int lr, int lc)
          {
              FirstRow = fr;
              FirstColumn = fc;
              LastRow = lr;
              LastColumn = lc;
          }
      }
  
      public RealRange RealUsedRange()
      {
          int FirstRow = xlWorkSheet.Cells.Find(
              "*",
              xlWorkSheet.get_Range("IV65536", misValue),
              Excel.XlFindLookIn.xlValues,
              Excel.XlLookAt.xlPart,
              Excel.XlSearchOrder.xlByRows,
              Excel.XlSearchDirection.xlNext,
              System.Reflection.Missing.Value,
              System.Reflection.Missing.Value,
              System.Reflection.Missing.Value
              ).Row;
  
          int FirstColumn = xlWorkSheet.Cells.Find(
              "*",
              xlWorkSheet.get_Range("IV65536", misValue),
              Excel.XlFindLookIn.xlValues,
              Excel.XlLookAt.xlPart,
              Excel.XlSearchOrder.xlByColumns,
              Excel.XlSearchDirection.xlNext,
              System.Reflection.Missing.Value,
              System.Reflection.Missing.Value,
              System.Reflection.Missing.Value
              ).Column;
  
          int LastRow = xlWorkSheet.Cells.Find(
              "*",
              xlWorkSheet.get_Range("IV65536", misValue),
              Excel.XlFindLookIn.xlValues,
              Excel.XlLookAt.xlPart,
              Excel.XlSearchOrder.xlByRows,
              Excel.XlSearchDirection.xlPrevious,
              System.Reflection.Missing.Value,
              System.Reflection.Missing.Value,
              System.Reflection.Missing.Value
              ).Row;
  
          int LastColumn = xlWorkSheet.Cells.Find(
              "*",
              xlWorkSheet.get_Range("IV65536", misValue),
              Excel.XlFindLookIn.xlValues,
              Excel.XlLookAt.xlPart,
              Excel.XlSearchOrder.xlByColumns,
              Excel.XlSearchDirection.xlPrevious,
              System.Reflection.Missing.Value,
              System.Reflection.Missing.Value,
              System.Reflection.Missing.Value
              ).Column;
  
         return new RealRange(FirstRow, FirstColumn, LastRow, LastColumn);
      }
  </code></pre>
  
  <p><strong>Solution</strong>
  Notice I have changed the return value from void to the struct RealRange. This is to make the result more readable. The 'range' in a spreadsheet is the span between 2 cells. You can copy the range by using the get_range-function like I did below. Thank you to D. Hilgarth for the help.</p>
  
  <pre><code>rr = RealUsedRange();
  this.workSheet.get_Range(
     this.workSheet.Cells[rr.FirstRow, rr.FirstColumn], 
     this.workSheet.Cells[rr.LastRow, rr.LastColumn]).Copy(missing);
  </code></pre>