检查VBA中的variant是否为null

时间:2021-05-08 16:25:41

I am passing a variant as parameter into a function

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

For simplicity I have put up this sample functions to explain my situation

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

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

Sometimes the FilesDetails will be null, during that time the function is returning "For loop not initialized" error

有时FilesDetails将为null,在此期间函数返回“For loop not initialized”错误

How can I check if the variant is null?

如何检查变量是否为空?

I tried UBound(FilesDetails), FilesDetails.Count,IsNull(FilesDetails) but no luck

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

3 个解决方案

#1


8  

I solved it by using the below method

我用下面的方法解决了这个问题

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

The isArrayAllocated function returns true if its not null

如果isArrayAllocated函数不为null,则返回true

#2


4  

Try this :-)

尝试这个 :-)

Dim var As Variant

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

or

要么

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

#3


1  

Try this:

尝试这个:

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

#1


8  

I solved it by using the below method

我用下面的方法解决了这个问题

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

The isArrayAllocated function returns true if its not null

如果isArrayAllocated函数不为null,则返回true

#2


4  

Try this :-)

尝试这个 :-)

Dim var As Variant

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

or

要么

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

#3


1  

Try this:

尝试这个:

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