简体   繁体   English

VBA变体 - 空数组的IsNull和IsEmpty都为false

[英]VBA Variant - IsNull and IsEmpty both false for empty array

I have a VBA class that contains a number of variants. 我有一个包含许多变体的VBA类。 These variants are there to hold Y x Z size arrays, but they're optional inputs (and so often the variants will never get used or initialized). 这些变体用于保存Y x Z大小的数组,但它们是可选输入(因此通常不会使用或初始化变体)。

A function within the same class calls on each of these variants to perform a calculation. 同一类中的函数调用这些变体中的每一个来执行计算。 BUT, I need it to skip the variants that are blank. 但是,我需要它跳过空白的变体。 When I try to use IsNull(xyz) and IsEmpty(xyz), both return false. 当我尝试使用IsNull(xyz)和IsEmpty(xyz)时,都返回false。 Looking at the watch on xyz, it has: 看着xyz上的手表,它有:

Expression: xyz
Value:
Type: Variant/Variant()
Context: className

Totally blank under value. 价值完全空白。

Any ideas how I can test/return a boolean if these things are empty? 如果这些东西是空的,我可以测试/返回一个布尔值的任何想法? Or even a boolean if they're full, that would work too. 或者如果它们已满,甚至是布尔值,这也会起作用。

Thanks 谢谢

Edit: I should add, that IsMissing just completely crashes excel... 编辑:我应该补充说,IsMissing只是完全崩溃excel ...

Very dirty workaround but something like this might do the trick: 非常脏的解决方法,但这样的事情可能会做的伎俩:

Function IsEmptyArray(testArr As Variant) As Boolean

Dim test As Long
Dim ret As Boolean

ret = False

    On Error Resume Next
    test = UBound(testArr)

    If Err.Number = 9 Then
        ret = True
    End If

    Err.Clear
    On Error GoTo 0

    IsEmptyArray = ret

End Function

One method I've used in the past is to test whether or not the array is filled using a helper function. 我过去使用的一种方法是测试是否使用辅助函数填充数组。 I join the array using an empty string delimiter and test that the length is greater than zero. 我使用空字符串分隔符连接数组并测试长度大于零。 It has worked in my circumstances, but I'm not sure if there are any flaws in the logic. 它在我的情况下起作用,但我不确定逻辑中是否存在任何缺陷。

Below code returns true if the array is empty and false if it is not: 如果数组为空,则下面的代码返回true,如果不是,则返回false:

Function TestIfArrayIsEmpty(vArray As Variant) As Boolean
    TestIfArrayIsEmpty = (Len(Join(vArray, "")) = 0)
End Function

You can use vartype(variable_name. You can check vartype(varriable_name) = vbArray or VbEmpty or VbNull Then check LBound and UBound of eac dimension for example 您可以使用vartype(variable_name。您可以检查vartype(varriable_name)= vbArray或VbEmpty或VbNull然后检查eac维度的LBound和UBound,例如

LBound(variable, 1) and UBound(variable_name, 1) to check the lower and higher indexes of the array. LBound(变量,1)和UBound(变量名,1)来检查数组的较低和较高索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM