尝试这个
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