2010-10-26 20 views
1

我想檢測是否設置了整數,如果不是,跳過循環中的大部分代碼(使用if語句)。這是我的目的。如何在Excel VBA中將整數設置爲空?

Do While hws.Cells(r, 9).Value <> "" 
    On Error Resume Next 
    ar = Null 
    ar = aws.Range("A:A").Find(hws.Cells(r, 2).Value).Row 
    If Not IsNull(ar) Then 
    'work with ar' 
    End If 
    r = r + 1 
Loop 

然而,當我運行它,ar = Null有問題。它說「無效使用null」。

回答

1

只使用一個變種及的isEmpty:

Dim i 

If IsEmpty(i) Then MsgBox "IsEmpty" 
i = 10 

If IsEmpty(i) Then 
    MsgBox "IsEmpty" 
Else 
    MsgBox "not Empty" 
End If 
i = Empty 
If IsEmpty(i) Then MsgBox "IsEmpty" 
'a kind of Nullable behaviour you only can get with a variant 
'do you have integer? 
Dim j as Integer 
j = 0 
If j = 0 Then MsgBox "j is 0" 
+0

不,謝謝,我喜歡一切定義。我可以將它重新設置爲空嗎? – 2010-10-26 12:06:14

+1

那麼你的代碼中的變量「ar」是什麼類型呢? – OlimilOops 2010-10-26 12:10:25

+0

它是一個整數。 – 2010-10-26 12:12:23

7

定義爲整型變量不能在VBA爲Null。你將不得不尋找另一種方式去做你想做的事。例如使用不同的數據類型或使用幻數來表示空(例如-1)。

在你的示例代碼中,ar將被分配一個Long值(Range.Row是一個Long),否則它會拋出一個錯誤。

相關問題