Arrays 如何在VBA中检查数组是否为空?
我有一个函数,它检查数组是否为空。从今天起,我得到一个运行时错误9。我不知道为什么 代码如下:Arrays 如何在VBA中检查数组是否为空?,arrays,vba,excel,Arrays,Vba,Excel,我有一个函数,它检查数组是否为空。从今天起,我得到一个运行时错误9。我不知道为什么 代码如下: 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
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
如何检查数组是否为空?首先使用isArray(arrItems)检查
然后检查ubound好的,我还没有找到更好的解决方案:
With rs
If Not .EOF Then
arrItems = .GetRows
Else
arrItems = Array()
End If
.Close
End With
ArrEmpty = IsArrayEmpty(arrItems)
Chip Pearson的网站上有一个功能一直对我有用
公共函数IsArrayEmpty(Arr作为变量)作为布尔值
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
“我饿了
'此函数测试数组是否为空(未分配)。返回TRUE或FALSE。
'
'VBA IsArray函数指示变量是否为数组,但它不是
'区分已分配数组和未分配数组。这两种情况都会恢复为真
'已分配和未分配的数组。此函数用于测试数组是否实际
“已经分配了。
'
'此功能实际上与IsArrayalLocation相反。
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
暗磅一样长
长得一样
呃,明白了
出错时继续下一步
如果IsArray(Arr)=False,则
'我们没有通过数组,返回True
IsArrayEmpty=True
如果结束
'尝试获取数组的UBound。如果数组是
'未分配,将发生错误。
UB=UBound(Arr,1)
如果(错误编号0),则
IsArrayEmpty=True
其他的
''''''''''''''''''''''''''''''''''''''''''
“在罕见的情况下,我
'无法可靠地答复,错误号
'对于未分配的空数组将为0。
'在这些情况下,LBound是0和
“乌邦是-1。
“为了适应这种奇怪的行为,测试
'看看LB>UB。如果是,则数组不可用
“分配。
''''''''''''''''''''''''''''''''''''''''''
呃,明白了
LB=LBound(Arr)
如果LB>UB那么
IsArrayEmpty=True
其他的
IsArrayEmpty=False
如果结束
如果结束
端函数
我是一名C#编码器,我对VBA在数组中的糟糕表现感到震惊。我采取了不同的方法。我发现Split()工作得非常好,而且效率很高,所以没有从函数中作为数组传递,而是构建了一个CSV字符串,并在处理之前测试它是否为空
我把它放在这里是希望这个模式能帮助一些人,像我一样,认为VBA对数组的处理很糟糕,不想在内存中到处翻找或使用“黑客”,我相信这是可行的,但从编程的角度来看,这似乎不令人满意
Private Function GetCsvOfStrings() As String
Dim strDelim As String
Dim strCsv As String
strDelim = ","
strCsv = ""
Dim i As Integer
For i = 1 To 10
If Len(strCsv) > 0 Then
strCsv = strCsv & strDelim & "test string (" & i & ")"
Else ' it's an empty string
strCsv = "test string (" & i & ")"
End If
Next i
GetCsvOfStrings = strCsv
End Function
Public Sub Test()
Dim strCsv As String
Dim strArr() As String
strCsv = GetCsvOfStrings()
If Len(strCsv) > 0 Then 'I've got an "array" of strings
strArr = Split(strCsv, ",")
Dim i As Integer
For i = LBound(strArr) To UBound(strArr)
Debug.Print (strArr(i))
Next i
End If
End Sub
我现在用
isArray(parArray)
检查了可能的重复项,它返回我true
,但是ubound不起作用。可能数组是二维的
Private Function GetCsvOfStrings() As String
Dim strDelim As String
Dim strCsv As String
strDelim = ","
strCsv = ""
Dim i As Integer
For i = 1 To 10
If Len(strCsv) > 0 Then
strCsv = strCsv & strDelim & "test string (" & i & ")"
Else ' it's an empty string
strCsv = "test string (" & i & ")"
End If
Next i
GetCsvOfStrings = strCsv
End Function
Public Sub Test()
Dim strCsv As String
Dim strArr() As String
strCsv = GetCsvOfStrings()
If Len(strCsv) > 0 Then 'I've got an "array" of strings
strArr = Split(strCsv, ",")
Dim i As Integer
For i = LBound(strArr) To UBound(strArr)
Debug.Print (strArr(i))
Next i
End If
End Sub