检查VBA中的variant是否为null

时间:2015-05-04 07:20:50

标签: vba excel-vba excel

我将变量作为参数传递给函数

为简单起见,我已经提出了这个示例函数来解释我的情况

Public Function test()
Dim arr() As String
testing (arr)
End Function

Public Function testing(ar As Variant)
If ar = Empty Then
    MsgBox ("Hello")
End If
End Function

有时FilesDetails将是null,在此期间功能正在返回" For loop not initialized"错误

如何检查变体是否为空?

我试过UBound(FilesDetails), FilesDetails.Count,IsNull(FilesDetails)但没有运气

4 个答案:

答案 0 :(得分:10)

我使用以下方法解决了它

Public Function test()
Dim Arr(1) As String
Arr(0) = "d"
Dim x As Boolean
x = IsArrayAllocated(Arr)
End Function


Function IsArrayAllocated(Arr As Variant) As Boolean
        On Error Resume Next
        IsArrayAllocated = IsArray(Arr) And _
                           Not IsError(LBound(Arr, 1)) And _
                           LBound(Arr, 1) <= UBound(Arr, 1)
End Function

如果isArrayAllocated函数非空

,则返回true

答案 1 :(得分:5)

试试这个: - )

Dim var As Variant

If var = Empty Then
    MsgBox "Variant data is Empty"
End If

If IsEmpty(var) Then
   MsgBox "Variant data is Empty"
End If

答案 2 :(得分:1)

试试这个:

Public Function test()
   Dim arr() As String
   testing (arr)
End Function

Public Function testing(ar As Variant)
 If arr = "" Then
      MsgBox ("Hello")
    End If
End Function

答案 3 :(得分:0)

我会使用 IsNull 函数来检查。