5

All works fine with a one-area range: Select cells A1 and B1 and give it the name "foo".

?ThisWorkbook.Names.Item("foo").RefersTo
 =Tabelle1!$A$1:$B$1
?ThisWorkbook.Names.Item("foo").RefersToRange.Address
 $A$1:$B$1

However, two-area ranges will fail (Excel 2010): Click cells A2 and B2 with ctrl pressed and give it the name "bar".

 ?ThisWorkbook.Names.Item("bar").RefersTo
  =Tabelle1!$A$2,Tabelle1!$B$2
 ?ThisWorkbook.Names.Item("bar").RefersToRange.Address
  -> Run-time error '1004'

Why can't we get the range the "bar"-name refers to?

Is there a workaround not using sheet.Range("bar") as I don't know which sheet holds the name?

4
  • Note .RefersToRange.Address appears to work as desired when I tested in Excel 2003. Commented Jan 11, 2013 at 15:01
  • @A.Webb I've tested on 2003 as well and it fails. Are you sure you created a multiarea range? Commented Jan 11, 2013 at 15:14
  • @GSerg Yes, I get, for example ?ThisWorkbook.Names.Item("bar").RefersToRange.Address $A$1,$C$1 Commented Jan 11, 2013 at 21:00
  • 3
    @A.Webb Ah. It would appear this works indeed if the Excel language is English. Doesn't work on [at least some] localized versions. Apparently this internally retrieves range address in local form and then treats it in en-us way (or the other way round). Another PITA Excel bug for my collection. Commented Jan 11, 2013 at 21:08

2 Answers 2

1

You could try the global Range, not a sheet.Range:

? Range(ThisWorkbook.Names("bar").RefersTo).Address

In order to avoid possible cross-workbook gotchas you can use ConvertFormula to add workbook name to the range:

? ThisWorkbook.Names("bar").RefersTo
  =Sheet1!$A$2,Sheet1!$B$2

? Application.ConvertFormula(ThisWorkbook.Names("bar").RefersTo, xlA1, xlA1)
  =[Book1]Sheet1!$A$2,[Book1]Sheet1!$B$2

? Application.Range(Application.ConvertFormula(ThisWorkbook.Names("bar").RefersTo, xlA1, xlA1)).Address
  =$A$2,$B$2

Sign up to request clarification or add additional context in comments.

4 Comments

Global range refers to the active workbook. There may be multiple workbooks cross-accessing named ranges, though that is not my use case, currently.
I know that. There is no Workbook.Range unfortunately. I'm actually quite surprised to learn RefersToRange falis on multiarea addresses. I suppose you're not going to parse the RefersTo yourself?
No way, no brittle string operations.
@amadeus No, actually doesn't look good. It always appends the active workbook name, not the name of the workbook to which the range belongs. Which is understandable if you think about it. Sadly, even relative R1C1 addressing does not help if I pass the correct anchor cell as the last parameter to ConvertFormula, it would still use the active workbook name.
0

A wrinkle here is that RefersToRange fails for a multi-area named range defined by:

=Sheet1!$B$2 + Sheet1!$D$4

But it will work for:

=Sheet1!$B$2 , Sheet1!$D$4

The difference being , vs +.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.