Named Range Address

Is there a way to get an excel address from a named Range. When i break on my named range object in Visual Studio i can see in the Non Public Members that there is a property with the string value of A4:D8. But i cannot seem to find a way to get it.


A round about way i currently have is to get the first cell and then the last cell and get the name from each cell and concatenate them into a string for my use. There should be a much easier way to get to this address as a property from the Range object.

Thanks,
Mark

Hi,

You can find all the named ranges using the following code.

C#


Range[] allRanges=workbook.Worksheets.GetNamedRanges();

Sorry that’s not what I’m looking for I can get the named range just fine. But is there and easy way from the named range to gets its excel address like this “A2:B8”.


Example code c#
Range range = MyWorkBook.Worksheets.GetRangeByName(myNamedRange);
string excelAddress = range.Address();
-----------------------------------------------
With the value of excelAddress being “A2:B8”.

Hi,

Please use this code to get the address of the given range.

C#


string startCell = CellsHelper.CellIndexToName(range.FirstRow, range.FirstColumn);

string endCell = CellsHelper.CellIndexToName(range.FirstRow + range.RowCount - 1, range.FirstColumn + range.ColumnCount - 1);


string address = startCell + “:” + endCell;

Hi,


Moreover, you may also try to perform your desired task,
e.g
Sample code:

Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook(“e:\test\Test.xls”);
//Browse All the Named RangeCollection in the workbook
foreach (Range range in workbook.Worksheets.GetNamedRanges())
{
string name = range.Name;
string address = workbook.Worksheets.Names[name].RefersTo;
}

workbook.Worksheets.Names[name].RefersTo;


Now this might be the thing im looking for. I will test this out!

Thanks very much i knew there had to be a way to get just the string address without getting start and end cells. I was already doing that as a work around so the previous post was of no use.