检查 VBA 中的变体是否为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30024638/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 09:34:39  来源:igfitidea点击:

Check if variant is null in VBA

vbaexcel-vbaexcel

提问by Vignesh Subramanian

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 FilesDetailswill be null, during that time the function is returning "For loop not initialized" error

有时FilesDetailsnull,在此期间,该函数返回“ 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)但没有运气

回答by Vignesh Subramanian

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

回答by daniele3004

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

回答by daniele3004

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