This project has moved. For the latest updates, please go here.

Excel C# UDF Range Parameter

Jul 27, 2012 at 4:29 PM

I'm trying to create a UDF in C# that will pull in a range of cells and do something with the contents of those cells.  I cannot seem to get the correct fype for the formula parameter though.  I have other formulas that are successfully pulling in string parameters but I need to support a range of cell references too.  Examples:

WORKS:

public string myFormula1(string myParameter)

{

return myParameter;

}

DOES NOT WORK:

public string myFormula(NetOffice.ExcelApi.Range myRange)       

{  

var rowCount = myRange.Rows.Count;          

return rowCount.ToString();       

}

 

What is the correct type for the range of cells argument?

Coordinator
Jul 27, 2012 at 9:23 PM
Edited Jul 27, 2012 at 9:27 PM

just have a look: http://blogs.msdn.com/b/eric_carter/archive/2004/12/01/273127.aspx
you can see:

public double CalculateArea(object Range)
{
  Excel.Range r = Range as Excel.Range;
  return Convert.ToDouble(r.Width) * Convert.ToDouble(r.Height);
}

in NetOffice you have to use:

public
double CalculateArea(object Range)
{
  Excel.Range r = new Excel.Range(null, Range);
  int result = Convert.ToDouble(r.Width) * Convert.ToDouble(r.Height);
   r.Dispose();
   return result;
}