2010-10-26 19 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),否则它会抛出一个错误。

相关问题