嘗試此
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](https://i.stack.imgur.com/aYtkk.jpg)
隨訪
你的代碼假設數組只存儲字符串嗎? - 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
你的代碼假設數組只存儲字符串嗎? – TPR
@TPG:更新我的文章 –