VBA:当数组为空时不要进入循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9874086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VBA: Don't go into loop when array is empty
提问by user1283776
I have a loop that can look like this:
我有一个看起来像这样的循环:
For Each article In artAll
Next
or like this:
或者像这样:
For i = 0 To Ubound(artAll)
Next
When the array length is 0, I get an error message. What is a good way to skip the loop when the array is empty? I suspect that I should use
当数组长度为 0 时,我收到一条错误消息。当数组为空时跳过循环的好方法是什么?我怀疑我应该使用
On Error Goto
but I need help finalizing a solution.
但我需要帮助最终确定解决方案。
回答by Dan
If Len(Join(artAll, "")) = 0 Then
'your for loops here
Should work
应该管用
回答by assylias
I use this function to test for empty arrays:
我使用这个函数来测试空数组:
Public Function isArrayEmpty(parArray As Variant) As Boolean
'Returns false if not an array or dynamic array that has not been initialised (ReDim) or 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 Function
Then in your main code:
然后在你的主代码中:
If isArrayEmpty(yourArray) Then
'do something - typically:
MsgBox "Empty Array"
Exit Function
End If
For i = LBound(yourArray,1) To UBound(yourArray,1)
'do something
Next i
回答by DavitosanX
This is an old question, but I found this solution to the problem, and it could be helpful to others:
这是一个老问题,但我找到了这个问题的解决方案,它可能对其他人有帮助:
If (Not myArray) = True Then
'Undimensionalized array. Respond as needed.
Else
'Array isn't empty, you can run your loop.
End If
It helped my out in a recent project, and found it to be very handy.
它在最近的一个项目中帮助了我,并发现它非常方便。
回答by markblandford
I like the solution given by @Dan but thought I would throw out there how I would normally handle an undimensionalized array:
我喜欢@Dan 给出的解决方案,但我想我会扔掉我通常如何处理无量纲化数组:
Dim lngUboundTest As Long
lngUboundTest = -1
On Error Resume Next
lngUboundTest = UBound(artAll)
On Error GoTo 0
If lngUboundTest >= 0 Then
'Your loop...
回答by TorontoJim
I found this thread looking for a solution to a problem where looping through a multidimensional array would fail if a dimensioned element was empty. I created the array by looping through a source that could have up to 6 datasets. Then after processing I would repeat this 19 more times.
我发现这个线程正在寻找一个问题的解决方案,如果一个维度元素为空,循环遍历多维数组将失败。我通过循环一个可能有多达 6 个数据集的源来创建数组。然后在处理后,我会再重复 19 次。
Dim varDeskData As Variant
Dim varDesk As Variant
ReDim varDesk(1 To 6)
For y = 1 To 6
ReDim varDeskData(1 To 4)
varDeskData(1) = "nifty integer from source(y)"
varDeskData(2) = "nifty string from source(y)"
varDeskData(3) = "another nifty string from source(y)"
varDeskData(4) = "another nifty string from source(y)"
varDesk(y) = varDeskData
Next y
When I ran the following, I would get the first three processed but then it would fail on the fourth, because I had only loaded three into the parent array:
当我运行以下命令时,我会处理前三个,但第四个会失败,因为我只将三个加载到父数组中:
For y = 1 To 6
If varDesk(y)(1) > 0 Then
... do nifty stuff ...
End If
End If
Using the IsEmpty procedure on the top level elements of the parent array fixed this:
在父数组的顶级元素上使用 IsEmpty 过程修复了这个问题:
For y = 1 To 6
If IsEmpty(varDesk(y)) = False Then
If varDesk(y)(1) > 0 Then
... do nifty stuff ...
End If
End If
End If