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 を返すように感じることです。それを確認するにはどうすればよいですか?

(文字列 s をバイト配列に等しく設定しようとしました。それは "" でした。つまり、配列は空ですよね?)

4

3 に答える 3

7

私は個人的にこれを使用しています - をReDim含む配列の場合ReDim v (1 To 5) As Variant、アイテムが 5 つisArrayEmpty(v)あるため false が返されますvが、それらはすべて初期化されていません。

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
于 2012-05-12T10:04:47.527 に答える
6

偉大な Chip Pearson のコードの下に貼り付けているだけです。配列関数に関する
彼のページもチェックしてください。

これが役立つことを願っています。

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
于 2013-09-30T13:56:26.087 に答える
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

スナップショット

ここに画像の説明を入力

ファローアップ

あなたのコードは、配列が文字列のみを格納していると想定していますか? – 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
于 2012-05-12T00:05:35.010 に答える