2017-08-24 48 views
0

当第一次调用函数时,我尝试启动静态变量为1。如何正确地做到这一点?这是类型不匹配错误。VBA如何检查变量是否已设置?

Static clip_success As Integer 
If clip_success Is Nothing Then 
    clip_success = 1 
End If 
+4

只要你定义它'clip_success'将有0'的'的值。 – YowE3K

+3

'如果clip_success = 0那么' –

+0

好的,谢谢你的回复。 – user1141649

回答

4

任何原始值类型将使用其默认值进行初始化。对于值为0的数字类型;对于字符串,这是""(一个空字符串);日期,这是1899-12-30。 A Boolean初始化为False

你的静态变量看起来非常像一个标志 - 应该可能是Boolean

A Variant用特殊值Empty初始化。

任何对象引用用Nothing/null引用初始化。


所以:

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