Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get sheet name from a named range's Name object

I have:

Microsoft.Office.Interop.Excel.Workbook wb;
Microsoft.Office.Interop.Excel.Name name;

Is there any way to get the worksheet name that the named range is on in the given workbook, assuming I've gotten the named range's Name object and wb already?

like image 718
Shark Avatar asked Nov 14 '11 21:11

Shark


People also ask

How do I extract a sheet name in Excel?

Sheet name code Excel formula Step 1: Type “CELL(“filename”,A1)”. The cell function is used to get the full filename and path. This function returns the filename of . xls workbook, including the sheet name.

How do you reference a specific cell in a named range?

The easiest is using the reference window while working on an Excel worksheet. In the upper left portion of the Excel environment is a small box which contains the cell name of the selected cell. A1, C10, etc. Click inside this box and type in a name then hit enter.

How do I find the name of a sheet?

We can use the CELL Function to return the file path, name, and sheet by inputting “filename”. To get the current worksheet's name, you can use the function with or without the optional reference argument, referring to any cell on the current tab.


2 Answers

Yes, use the Parent property to work your way up the object hierarchy:

ws = name.RefersToRange.Parent.name;
like image 60
Raymond Hettinger Avatar answered Sep 30 '22 16:09

Raymond Hettinger


Range.Worksheet is a self-documenting alternative to Range.Parent:

string wsName = name.RefersToRange.Worksheet.Name;


(Or in 2 steps:

Microsoft.Office.Interop.Excel.Worksheet ws = name.RefersToRange.Worksheet;
string wsName = ws.Name;

)

Reference:
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.name.referstorange.aspx
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.range.worksheet.aspx
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel._worksheet.name(v=office.15).aspx

like image 24
Aaron Thoma Avatar answered Sep 30 '22 17:09

Aaron Thoma