Shark
Shark

Reputation: 2382

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?

Upvotes: 8

Views: 27401

Answers (3)

Aaron Thoma
Aaron Thoma

Reputation: 4026

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

Upvotes: 5

Doug Glancy
Doug Glancy

Reputation: 27478

wb.Names(name).RefersToRange.Parent.Name

Upvotes: 0

Raymond Hettinger
Raymond Hettinger

Reputation: 226346

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

ws = name.RefersToRange.Parent.name;

Upvotes: 16

Related Questions