在VBA中使用Excel定义的名称 [英] Using Excel Defined Names in VBA
问题描述
在VBA中,我可以成功使用引用单元格的Excel定义名称。 例如,如果我定义名称为"ValueInA1",引用单元格A1,则以下VBA代码将在消息框中显示单元格A1的内容。
In VBA, I can successfully use an Excel defined name which refers to a cell. For instance, if I define a name "ValueInA1", which refers to cell A1, the following VBA code will display the contents of cell A1 in a message box.
Variable1 =范围("ValueInA1")
Variable1 = Range("ValueInA1")
MsgBox = Variable1
MsgBox = Variable1
但是,如果我创建包含常量或公式的已定义名称,并尝试使用它如上所述,它在VBA中生成运行时错误"1004",对象"_Global"的注释"Method'Range"失败。 你能告诉我如何在VBA中使用定义名称
引用常量或公式吗?
However if I create a defined name containing a constant or formula, and try to use it in VBA as above, it generates a Run-time error '1004', with the comment "Method 'Range' of object '_Global' failed. Can you tell me how to use defined names referring to constants or formulas, in VBA?
推荐答案
>
" MsgBox"不是变量,不要在MsgBox之后使用等号(=)。
Hi,
"MsgBox" is not a variable, don't use equal(=) after MsgBox.
' -- sample 1 (redundant a little) ---
Dim Variable1 As String
Variable1 = Range("ValueInA1")
MsgBox Variable1
' -- sample 2 (simplest) ---
MsgBox Range("ValueInA1")
问候。
Regards.
这篇关于在VBA中使用Excel定义的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!