2016-08-05 18 views
1

私は、配列が空であるかどうかをチェックする関数を持っています。今日から、ランタイムエラーが発生しています。なぜか分かりません。ここで VBAで配列が空であるかどうかをチェックする方法?

はコードです:

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 

配列が空の場合はどうすれば確認できますか?

+2

http://stackoverflow.com/questions/206324/how-to-check-for-empty-array-in-vba-macro –

+0

可能なDのために働い[vbaマクロで空の配列をチェックする方法](http://stackoverflow.com/questions/206324/how-to-check-for-empty-array-in-vba-macro) – LWC

答えて

0

は、[OK]を私はこのような、より良い解決策を見つけていないUBOUNDを確認してください。常に持っているチップ・ピアソンのウェブサイト上の機能があります

With rs 
    If Not .EOF Then 
     arrItems = .GetRows 
    Else 
     arrItems = Array() 
    End If 
    .Close 
End With 

ArrEmpty = IsArrayEmpty(arrItems) 
1

チェック最初でIsArray(arrItems)との はその後

+1

私は今すぐチェックした'isArray(parArray)'そして 'true'を返しますが、uboundは機能しません。 – yuro

+0

おそらく配列は2次元です – Kelaref

1

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 
関連する問題