Excel VBA worksheet.names vs worksheet.range

偶尔善良 提交于 2019-12-17 16:52:58

问题


I have created a defined name/range on a worksheet called bob, pointing to a single cell. There are a number of other name/ranges set up on this worksheet, which I didn't create. All the number/ranges work perfectly except for mine.

I should be able to refer to the contents of this cell by using either of the following statements:

(worksheet object).Names("bob").RefersToRange.Value
(worksheet object).Range("bob").Value

However, only the second statement, referring to the Range works for some reason. The first one can't find the name in the Names list.

My questions are:

  1. What is the difference, if any, between a Name and a Range?
  2. Is this something to do with the global/local scope of my name/range?
  3. How were the other name/ranges created on the sheet so that they appear in both the worksheets Name and Range list?

回答1:


Yes, you are right. Names can be local (belong to a worksheet) and global (belong to a workbook).

(worksheet object).Names("bob") will only find a local name. Your name is obviously global so you could access it as (worksheet object).Workbook.Names("bob").RefersToRange.

The "other names" are probably local. They only appear in the ranges list when their parent sheet is active (check that out). To create a local name, prepend it with the sheet name, separated by a '!': 'My Sheet Name'!bob.




回答2:


I don't know how to do it with code, but if you go to the Name Manager Under the Formula tab group in the Ribbon in Excel 2007, you can create names and choose their scope.



来源:https://stackoverflow.com/questions/2175962/excel-vba-worksheet-names-vs-worksheet-range

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!