VBA的奇怪行为:
以下代码中的第三行会出现错误94“Null的非法使用”:
Function test1()
Dim a As String
a = Null
test1 = a
End Function
但是...这个完美地工作了:
Function test1()
Dim a, b As String
a = Null
test1 = a
End Function
唯一的区别在于第二行中的变量b从未被使用!这是怎么回事?
VBA的奇怪行为:
以下代码中的第三行会出现错误94“Null的非法使用”:
Function test1()
Dim a As String
a = Null
test1 = a
End Function
Function test1()
Dim a, b As String
a = Null
test1 = a
End Function
In the second code sample ...
Dim a, b As String
As String
只适用于最后一个变量 b,变量 a 是 Variant 类型。
Variant 类型的变量可以接受 Null 值。String 类型不能。
在第一个代码示例中,a 明确声明为 String 类型。这就是为什么它不会接受 Null 值。
如果你希望 a 和 b 都是 String 类型,你必须显式地为每个变量声明类型...
Dim a As String, b As String