vba 我如何使用 RefersToRange?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40459845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I use RefersToRange?
提问by Praveen Behera
Can anyone tell me how to use RefersToRange in vba? and what and when is the need of it.
谁能告诉我如何在 vba 中使用 RefersToRange?什么时候需要它。
Please provide with simple example first.
请先提供简单的例子。
Thanks in advance.
提前致谢。
采纳答案by Zev Spitz
In Excel, there is the concept of a named range, that is a range of cells which has a name attached to it. This is represented by the Nameobject.
在 Excel 中,有命名范围的概念,即具有附加名称的单元格范围。这由Name对象表示。
The RefersToRangemethod:
该RefersToRange方法:
Returns the Rangeobject referred to by a Name object.
返回Name 对象引用的Range对象。
For example, let's say I want to read the values only in the print area of the current worksheet. I need the Name
object in order to access the print area, but I can't do anything useful with it; if I want to do something useful I have to access the range of cells referred to by that name:
例如,假设我只想读取当前工作表打印区域中的值。我需要这个Name
对象才能访问打印区域,但我不能用它做任何有用的事情;如果我想做一些有用的事情,我必须访问该名称引用的单元格范围:
Dim n As Name
Set n = ActiveWorkbook.Names("Print_Area")
Dim rng As Range
Set rng = n.RefersToRange
Dim values() As Variant
values = rng 'I can't read values with a Name object, but I can with a Range object
回答by zrfrank
RefersToRange is, one of many ways, to 'point' a range. For example,
RefersToRange 是“指向”范围的多种方式之一。例如,
ThisWorkbook.Names("DownloadPictures").RefersToRange = "yes"
the above code, points to the range, which can be a cell, named "DownloadPictures" and sets it value to "yes". As a matter of fact, I'd rather use,
上面的代码指向范围,它可以是一个单元格,命名为“DownloadPictures”并将其值设置为“yes”。事实上,我宁愿使用,
ThisWorkbook.range("DownloadPictures").Value = "yes"
The 'Name' in MS Excel is similar to the Variant variable type, but unlike the variables you 'Dim' in the VB code, the 'Name' only be used (or you can consider it is only be used) in the Workbook. The 'Name' and variables are not interfered with each other.
MS Excel 中的“名称”类似于 Variant 变量类型,但与您在 VB 代码中“变暗”的变量不同,“名称”仅用于(或者您可以认为它仅用于)工作簿中。“名称”和变量不会相互干扰。
If you open the 'Name Manager', the concept becomes easier to understand. The Name can be created to refer to a cell, a range of cell (a range), some value (so called constant), or a formula.
如果您打开“名称管理器”,这个概念就会变得更容易理解。可以创建名称以引用单元格、单元格区域(范围)、某个值(所谓的常量)或公式。
Each Name object represents a defined name for a range of cells. Names can be either built-in names—such as Database, Print_Area, and Auto_Open—or custom names. ---MSDN
每个 Name 对象代表一系列单元格的定义名称。名称可以是内置名称(例如 Database、Print_Area 和 Auto_Open)或自定义名称。---MSDN
As a Name can refer to range, constant or a formula .RefersToRange
specifically refer to a range. And return false if not so.
作为名称可以指代范围,常量或公式.RefersToRange
专门指代一个范围。如果不是,则返回 false。