如何在VBA中检查数组是否为空?

时间:2016-08-05 08:16:17

标签: arrays vba excel-vba excel

我有一个函数,它检查数组是否为空。从今天起我遇到运行时错误9.我不知道为什么。

以下是代码:

When db table contains data, pass it to the variable => arrItems
arrItems as Variant
ArrEmpty as Boolean

With rs
  If Not .EOF Then
      arrItems = .GetRows
      .Close
  End If
End With

ArrEmpty = IsArrayEmpty(arrItems) 

Private Function IsArrayEmpty(parArray As Variant) As Boolean
    IsArrayEmpty = IIf(UBound(parArray) > 0, False, True) //Here is invoked the runtime error 9
End Function

如何检查数组是否为空?

4 个答案:

答案 0 :(得分:3)

Chip Pearson的网站上有一个功能,一直为我工作link

Public Function IsArrayEmpty(Arr As Variant) As Boolean
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' IsArrayEmpty
' This function tests whether the array is empty (unallocated). Returns TRUE or FALSE.
'
' The VBA IsArray function indicates whether a variable is an array, but it does not
' distinguish between allocated and unallocated arrays. It will return TRUE for both
' allocated and unallocated arrays. This function tests whether the array has actually
' been allocated.
'
' This function is really the reverse of IsArrayAllocated.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Dim LB As Long
Dim UB As Long

Err.Clear
On Error Resume Next
If IsArray(Arr) = False Then
    ' we weren't passed an array, return True
    IsArrayEmpty = True
End If

' Attempt to get the UBound of the array. If the array is
' unallocated, an error will occur.
UB = UBound(Arr, 1)
If (Err.Number <> 0) Then
    IsArrayEmpty = True
Else
    ''''''''''''''''''''''''''''''''''''''''''
    ' On rare occassion, under circumstances I
    ' cannot reliably replictate, Err.Number
    ' will be 0 for an unallocated, empty array.
    ' On these occassions, LBound is 0 and
    ' UBoung is -1.
    ' To accomodate the weird behavior, test to
    ' see if LB > UB. If so, the array is not
    ' allocated.
    ''''''''''''''''''''''''''''''''''''''''''
    Err.Clear
    LB = LBound(Arr)
    If LB > UB Then
        IsArrayEmpty = True
    Else
        IsArrayEmpty = False
    End If
End If

End Function

答案 1 :(得分:2)

首先使用isArray(arrItems)进行检查 然后查看未结束的

答案 2 :(得分:0)

好的,我还没有找到更好的解决方案:

$query = mysql_query("SELECT * 
                      FROM wrd_users 
                      WHERE emp_username='$user' 
                        and  emp_password='$pass' ");

if(mysql_num_rows($query) > 0 ) 
{ 
    //check if there is already an entry for that username

    $row=mysql_fetch_array($query);

    if($row['emp_type']=='1')
    {
        echo "<br>KING1";
    }

    if($row['emp_type']=='2')
    {
        echo "<br>KING2";
    }                      
}
else
{

    //header("location:index.php");
    echo(" No User Found");
    //header('work.php');
}

答案 3 :(得分:0)

我是一名C#编码员,对VBA在数组中的糟糕程度感到震惊。我采取了不同的方法。我发现Split()确实很好并且效率很高,所以没有从函数作为Array传递,而是构建了一个CSV字符串并在处理之前测试了它是否为空。

我将其放在此处的希望是,这种模式可以帮助像我一样认为VBA对数组的处理很糟糕并且不希望在内存中挖掘或使用“ hacks”的人(我敢肯定)工作,但似乎对编程POV感到不满意...

 Private Function GetCsvOfStrings() As String
    Dim strDelim As String
    Dim strCsv As String
    strDelim = ","
    strCsv = ""

    Dim i As Integer
    For i = 1 To 10
        If Len(strCsv) > 0 Then
             strCsv = strCsv & strDelim & "test string (" & i & ")"
        Else ' it's an empty string
             strCsv = "test string (" & i & ")"
        End If
    Next i
    GetCsvOfStrings = strCsv
End Function

Public Sub Test()
    Dim strCsv As String
    Dim strArr() As String
    strCsv = GetCsvOfStrings()
    If Len(strCsv) > 0 Then 'I've got an "array" of strings
        strArr = Split(strCsv, ",")
        Dim i As Integer
        For i = LBound(strArr) To UBound(strArr)
            Debug.Print (strArr(i))
        Next i
    End If
End Sub