vba 你如何让一个范围返回它的名字?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3630496/
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 you get a Range to return its Name?
提问by GenericJam
Dim sampleRange as Range
Set sampleRange = Worksheet.Range(Cells(1,1),Cells(1,4)
sampleRange.Name = "Range1"
MsgBox sampleRange.Name
The above code will show the actual address of the range, not the name. Why?
How do I get a named range to return its name?
上面的代码将显示范围的实际地址,而不是名称。为什么?
如何获取命名范围以返回其名称?
回答by Lance Roberts
For a Range, Name isn't a string it's a Name object, that you then take the Name property of to get the string:
对于 Range,Name 不是一个字符串,它是一个 Name 对象,然后您可以使用 Name 属性来获取字符串:
MsgBox sampleRange.Name.Name
回答by sirplus
sampleRange.Name = "Range1" to name the range is poor practice
sampleRange.Name = "Range1" 命名范围是不好的做法
It only works because Name (the string containing "The Name") is the default property of Name.
之所以有效,是因为 Name(包含“The Name”的字符串)是 Name 的默认属性。
Much better sampleRange.Name.Name = "Range1"
更好的 sampleRange.Name.Name = "Range1"
It's never good practice to use the default property without referring to it explicitly.
使用默认属性而不明确引用它从来都不是一个好习惯。
Good programming exactly controls the environment by referring explicitly to the property required.
好的编程通过明确地引用所需的属性来精确地控制环境。
Removes ambiguity as created the issue here. Eliminates the issue created if the default property is changed in a future update.
消除此处创建问题的歧义。消除在未来更新中更改默认属性时产生的问题。

