VBA 隐式和显式声明
示例
如果代码模块不包含OptionExplicit在模块顶部,则编译器将在使用它们时自动(即“隐式”)为您创建变量。它们将默认为变量类型Variant。
Public Sub ExampleDeclaration()
someVariable = 10 '
someOtherVariable = "Hello World"
'Both of these variables are of the Variant type.
End Sub在上面的代码,如果OptionExplicit指定,代码将中断,因为它缺少必要Dim的陈述someVariable和someOtherVariable。
Option Explicit
Public Sub ExampleDeclaration()
Dim someVariable As Long
someVariable = 10
Dim someOtherVariable As String
someOtherVariable = "Hello World"
End Sub最好的做法是在代码模块中使用OptionExplicit,以确保声明所有变量。
请参见VBA最佳做法,默认情况下如何设置此选项。
热门推荐
10 枣庄人事考试祝福语简短
11 免费生日祝福语简短独特
12 企业国庆问候祝福语简短
13 直系离职祝福语大全简短
14 早餐祝福语怎么写简短
15 学生真挚的祝福语简短
16 闺蜜好运祝福语简短
17 感动妈妈的祝福语简短
18 考试祝福语简短给对象