VBA 如何检查变量是否已设置?
VBA How to check variable if it is set?
我尝试在第一次调用函数时将静态变量初始化为 1。如何正确地做到这一点?这是类型不匹配错误。
Static clip_success As Integer
If clip_success Is Nothing Then
clip_success = 1
End If
任何原始值类型都将使用其默认值进行初始化。对于数值类型,值为 0
;对于字符串,这是 ""
(一个空字符串);对于日期,即 1899-12-30
。一个 Boolean
被初始化为 False
.
你的静态变量看起来很像一个标志 - 应该是 Boolean
.
A Variant
初始化为特殊值 Empty
。
任何对象引用都使用 Nothing
/空引用初始化。
所以:
Static clip_success As Long
If clip_success = 0 Then
clip_success = 1
End If
或
Static clip_success As Date
If clip_success = CDate(0) Then
clip_success = DateTime.Now
End If
或
Static clip_success As String
If clip_success = vbNullString Then
clip_success = "success!"
End If
或
Static clip_success As Variant
If IsEmpty(clip_success) Then
clip_success = 1
End If
或
Static clip_success As Object
If clip_success Is Nothing Then
Set clip_success = New [some class]
End If
我尝试在第一次调用函数时将静态变量初始化为 1。如何正确地做到这一点?这是类型不匹配错误。
Static clip_success As Integer
If clip_success Is Nothing Then
clip_success = 1
End If
任何原始值类型都将使用其默认值进行初始化。对于数值类型,值为 0
;对于字符串,这是 ""
(一个空字符串);对于日期,即 1899-12-30
。一个 Boolean
被初始化为 False
.
你的静态变量看起来很像一个标志 - 应该是 Boolean
.
A Variant
初始化为特殊值 Empty
。
任何对象引用都使用 Nothing
/空引用初始化。
所以:
Static clip_success As Long
If clip_success = 0 Then
clip_success = 1
End If
或
Static clip_success As Date
If clip_success = CDate(0) Then
clip_success = DateTime.Now
End If
或
Static clip_success As String
If clip_success = vbNullString Then
clip_success = "success!"
End If
或
Static clip_success As Variant
If IsEmpty(clip_success) Then
clip_success = 1
End If
或
Static clip_success As Object
If clip_success Is Nothing Then
Set clip_success = New [some class]
End If