在 Excel 2013 VBA 中引用命名单元格的正确方法是什么?(我知道我在搞砸)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17228318/
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
What's the RIGHT way to reference named cells in Excel 2013 VBA? (I know I'm messing this up)
提问by StolenKitten
I have a cell (Ok there's a bunch but I'm just looking at one now) named "Classes". It's "C10" by Excel's grid notation.
我有一个名为“类”的单元格(好吧,有一堆,但我现在只看一个)。Excel 的网格表示法是“C10”。
My code works perfectly when I reference the cell as
当我将单元格引用为时,我的代码运行良好
Range("C10") = "Value"
But when I use
但是当我使用
Classes = "Value"
It just does nothing.
它什么都不做。
So, what's the correct way to reference a named cell by its name?
那么,通过名称引用命名单元格的正确方法是什么?
回答by Jon Crowell
You replace the address with the named range's name:
您用命名范围的名称替换地址:
Range("Classes") = "Value"
回答by robotik
You can use the square bracket shorthand for referencing ranges:
您可以使用方括号简写来引用范围:
[Classes] = "Value"
So you can save on Range
and ""
, and it also looks better (square brackets look a bit like a cell). You can also use [C10] = "Value"
所以你可以保存Range
和""
,它看起来也更好(方括号看起来有点像一个单元格)。你也可以使用[C10] = "Value"