2017-08-24 47 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