2012-05-11 63 views
4
Function IsVarArrayEmpty(anArray As Variant) 

Dim i As Integer 

On Error Resume Next 
    i = UBound(anArray, 1) 
If Err.Number = 0 Then 
    IsVarArrayEmpty = False 
Else 
    IsVarArrayEmpty = True 
End If 

End Function 

它對未初始化返回true,對初始化爲false。我想看看它是否有任何數據/內容。但是,問題是我覺得即使數組中沒有數據,上面的代碼也會返回false。我如何檢查?vba - 檢查空陣列

(我想字符串s設置爲等於字節數組。這是 「」,這意味着該數組爲空,是嗎?)

回答

4

嘗試此

Sub Sample() 
    Dim Ar As Variant 
    Dim strTest As String 

    strg = "Blah" 
    Ar = Split(strg, "|") 
    Debug.Print "TEST1 : "; IsArrayEmpty(Ar) 

    strg = "Blah|Blah" 
    Ar = Split(strg, "|") 
    Debug.Print "TEST2 : "; IsArrayEmpty(Ar) 

End Sub 

Function IsArrayEmpty(Ar As Variant) As Boolean 
    If InStr(TypeName(Ar), "(") > 0 Then 
     If Not IsEmpty(Ar) Then 
      If UBound(Ar) > 0 Then 
       IsArrayEmpty = False 
      Else 
       IsArrayEmpty = True 
      End If 
     End If 
    End If 
End Function 

快照

enter image description here

隨訪

你的代碼假設數組只存儲字符串嗎? - TPG 7分鐘前

是的。它做了。如果你想測試所有條件,那麼我會建議使用API​​。下面是一個例子

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _ 
(pDst As Any, pSrc As Any, ByVal ByteLen As Long) 

Sub Sample() 
    Dim Ar() As Long 

    Debug.Print ArrayNotEmpty(Ar) '<~~ False 

    ReDim Ar(1) 

    Debug.Print ArrayNotEmpty(Ar) '<~~ True 
End Sub 

Public Function ArrayNotEmpty(Ar) As Boolean 
    Dim Ret As Long 

    CopyMemory Ret, ByVal VarPtr(Ar) + 8, ByVal 4 
    CopyMemory Ret, ByVal Ret, ByVal 4 
    ArrayNotEmpty = (Ret <> 0) 
End Function 
+0

你的代碼假設數組只存儲字符串嗎? – TPR

+0

@TPG:更新我的文章 –

6

我個人使用這一點 - 如果你現在ReDimReDim v (1 To 5) As Variant數組,isArrayEmpty(v)將返回false,因爲v有5個項目,雖然他們都是未初始化。

Public Function isArrayEmpty(parArray As Variant) As Boolean 
'Returns true if: 
' - parArray is not an array 
' - parArray is a dynamic array that has not been initialised (ReDim) 
' - parArray is a dynamic array has been erased (Erase) 

    If IsArray(parArray) = False Then isArrayEmpty = True 

    On Error Resume Next 

    If UBound(parArray) < LBound(parArray) Then 
     isArrayEmpty = True 
     Exit Function 
    Else 
     isArrayEmpty = False 
    End If 

End Function 
5

我只是簡單地粘貼在偉大的Chip Pearson的代碼下面。
也查看他的page on array functions

我希望這會有所幫助。

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 
     ' UBound 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 
+0

+1我一直在使用它一段時間,以及他的一些其他陣列幫助函數。 –

+0

您發佈了[相同的答案兩次](http://stackoverflow.com/a/19096020/119775)。 –