title: converting vba to VSTO and selecting used cells
body: <p>hello guys im trying to convert some existing code from VBA to VSTO, and i have been very successful thus far. 
  Here is the code  I'm trying to convert.</p>
  
  <pre><code>this is the vba code
  
  
  Sub FindUsedRange()
  Dim LastRow As Long
  Dim FirstRow As Long
  Dim LastCol As Integer
  Dim FirstCol As Integer
  
  ' Find the FIRST real row
  FirstRow = ActiveSheet.Cells.Find(What:="*", _
    SearchDirection:=xlNext, _
    SearchOrder:=xlByRows).Row
  
  ' Find the FIRST real column
  FirstCol = ActiveSheet.Cells.Find(What:="*", _
    SearchDirection:=xlNext, _
    SearchOrder:=xlByColumns).Column
  
  ' Find the LAST real row
  LastRow = ActiveSheet.Cells.Find(What:="*", _
    SearchDirection:=xlPrevious, _
    SearchOrder:=xlByRows).Row
  
  ' Find the LAST real column
  LastCol = ActiveSheet.Cells.Find(What:="*", _
    SearchDirection:=xlPrevious, _
    SearchOrder:=xlByColumns).Column
  
  'Select the ACTUAL Used Range as identified by the
  'variables identified above
  ActiveSheet.Range(Cells(FirstRow, FirstCol), _
      Cells(LastRow, LastCol)).Select
  End Sub
  </code></pre>
  
  <p>however when i get to this part </p>
  
  <pre><code> ActiveSheet.Range(Cells(FirstRow, FirstCol), _
          Cells(LastRow, LastCol)).Select
      End Sub
  </code></pre>
  
  <p>in .net i have no idea how to handle? does anyone have any idea and can point me in the right direction. </p>
  
  <p>here is my c# code for the addin I'm building in case anyone is insterested.</p>
  
  <pre><code>    using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Drawing;
  using System.Data;
  using System.Linq;
  using System.Text;
  using System.Windows.Forms;
  using Microsoft.Office.Interop.Excel;
  //using office.Excel;
  namespace ExcelAddIn3.TaskPane
  {
      public partial class TaskPaneView : UserControl
      {
          public TaskPaneView()
          {
              InitializeComponent();
          }
  
          private void button1_Click(object sender, EventArgs e)
          {
  
  var time = DateTime.Now.ToLongTimeString();
  
  this.label1.Text = time;
  System.Array arr;
  Microsoft.Office.Interop.Excel.Range  LastRow;
  int  firstRow;
  Microsoft.Office.Interop.Excel.Range  lastCol;
  Microsoft.Office.Interop.Excel.Range  firstCol;
  
  Microsoft.Office.Interop.Excel.Range currentFind = null;
  Microsoft.Office.Interop.Excel.Range firstFind = null;
  Microsoft.Office.Interop.Excel.Range fruits = ExcelAddIn3.Globals.ThisAddIn.Application.get_Range("A:Z");
  
  Microsoft.Office.Interop.Excel.Worksheet sheet;
  
  
  
  firstRow = fruits.Find("*", Type.Missing, Type.Missing,
                                               Type.Missing,
                                               Microsoft.Office.Interop.Excel.XlSearchOrder.xlByRows,
                                               Microsoft.Office.Interop.Excel.XlSearchDirection.xlNext, true,
                                               Type.Missing, Type.Missing).Row;
  
  firstCol = fruits.Find("*", Type.Missing, Type.Missing,
                                               Type.Missing,
                                               Microsoft.Office.Interop.Excel.XlSearchOrder.xlByColumns,
                                               Microsoft.Office.Interop.Excel.XlSearchDirection.xlNext, true,
                                               Type.Missing, Type.Missing);
  
  LastRow = fruits.Find("*", Type.Missing, Type.Missing,
                                              Type.Missing,
                                               Microsoft.Office.Interop.Excel.XlSearchOrder.xlByRows,
                                               Microsoft.Office.Interop.Excel.XlSearchDirection.xlPrevious, true,
                                               Type.Missing, Type.Missing);
  
  lastCol = fruits.Find("*", Type.Missing, Type.Missing,
                                              Type.Missing,
                                               Microsoft.Office.Interop.Excel.XlSearchOrder.xlByColumns,
                                               Microsoft.Office.Interop.Excel.XlSearchDirection.xlPrevious, true,
                                               Type.Missing, Type.Missing);
  
  
  while (lastCol != null)
  {
      if (firstFind == null)
      {
          firstFind = lastCol;
          System.Windows.Forms.MessageBox.Show(LastRow.get_Address() + "last Column.");
  
      }
      else
      {
          break;
      }
  }
  
  
  firstFind = null;          
  
   while (LastRow != null)
  {
      if (firstFind == null)
      {
          firstFind = LastRow;
          System.Windows.Forms.MessageBox.Show(LastRow.get_Address() + "last row.");
  
  
      }
      else
      {
          break;
      }
  }
  firstFind = null;
  while (firstCol != null)
  {
      if (firstFind == null)
      {
          firstFind = firstCol;
          System.Windows.Forms.MessageBox.Show(firstCol.get_Address() + "first real column.");
  
  
      }
      else
      {
          break; 
      }
  }
  
  
  firstFind = null;
  while(firstRow != null){
      if (firstFind == null)
      {
  
          System.Windows.Forms.MessageBox.Show(firstRow + "first real row.");
  
  
      }
      else 
          {
  
              break;
          }
  }
  
  
  
  Range currentCells = ExcelAddIn3.Globals.ThisAddIn.Application.get_Range(firstCol, lastCol);
  currentCells.Select();
  
          }
      }
  }
  </code></pre>
  
  <p>right now my current solution is </p>
  
  <pre><code>Range currentCells = ExcelAddIn3.Globals.ThisAddIn.Application.get_Range(firstCol, lastCol);
  currentCells.Select();
  </code></pre>
  
  <p>however this uses address as opposed to the ordinal notation of the vba code.</p>