Search code examples
c#excelexcel-dna

Getting my head around string manipulation


I've been working on an ExcelDNA/C# add in for a while, and I'm at the final hurdle.

I can get a selection address, and I first of all need to check if the rows in that selection are simply two, e.g. in Excel they would be 8+9, two rows next to each other or any two consecutive numbers.

I then need to check that there are more than two columns, etc C to J (more than two spaces in the alphabet).

This all needs to be done from a string like this: Sheet1!$C$8:$J$9

What I am trying to do, is split a selection like this, which returns the above string, into two strings, in the case of the example, the desired end result would be

Sheet1!$C$8:$J$8 + Sheet1!$C$9:$J$9 in two different strings, perhaps I need more coffee, but if anyone has a less trashy way of doing this than I plan, I would be forever in your debt!


Solution

  • Do you have a reference to the Range object? If so, range.Rows.Count == 2 will tell you if there are two rows, and range.Columns.Count > 2 will tell you if there are more than two columns.

    Then, to get the addresses of the two rows independently you can do something like:-

    var address1 = range.Rows[1].Address(external:true);
    var address2 = range.Rows[2].Address(external:true);