0

I am developing an Excel Addin! I want to create an union of cells (range) without using Xlapp.Union(recursive way), by just giving couples (x,y) of cells in one string!

I tried using XLapp.Union but it's realy slow, then I am looking for a new method to do the same thing just by giving cellsAdress as string.

I am looking for a function that takes in params a string: something like this ( "A1,B3,C5...") and wich retruns a range!

I tried: Range RE = ExcelWorkbook.ActiveSheet.get_Range(RgEvenString, Type.Missing); where RgEvenString is a string like : "A1,B3,C5..." which throw an exception!!

Can anyone help plz!

Kyle Pittman
  • 2,858
  • 1
  • 30
  • 38

2 Answers2

1

http://social.msdn.microsoft.com/Forums/en-US/abf5a953-caa2-44c1-9723-fb7b1d99f2fc/how-to-reference-noncontiguous-selected-rows-in-excel?forum=isvvba

Check out this link for a way to reference noncontiguous cells in Excel. The link is in VBA but you should be able to apply it to C#. Consider mimicking the "selection" from that post by creating your own "selection" from parsing the string containing the cell addresses.

By the way, how slow is Union? Can you post an example of the way you're using the Union function in context with the other code? Maybe there's something else that can be optimized.

Kyle Pittman
  • 2,858
  • 1
  • 30
  • 38
-1
public static Range OldUnionRange(List<System.Drawing.Point> list,Range range, Boolean Even){
Range RgEven = null;
Boolean RgEvenBool = false;
Range RgOdd = null;
Boolean RgOddBool = false;
for (int j = 0; j < list.Count; j++){
System.Drawing.Point p = list[j];
int x = p.X;
int y = p.Y;
if (x % 2 == 0 && Even)
{Range rng = range.Cells[x + 1, y + 1];
if (!RgEvenBool){
RgEven = range.Cells[x + 1, y + 1];
RgEvenBool = true;
}
RgEven = Shuttle_add_in_Loader.XlApp.Union(RgEven, rng);
}
else if (x % 2 != 0 && !Even)
{Range rng = range.Cells[x + 1, y + 1];
if (!RgOddBool)
{
RgOdd = range.Cells[x + 1, y + 1];
RgOddBool = true;
}
RgOdd = Shuttle_add_in_Loader.XlApp.Union(RgOdd, rng);
}
}
if (Even)
{
return RgEven;
}
else
{return RgOdd;}
}