1

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

On Error Goto

but I need help finalizing a solution.

Gaffi
  • 4,307
  • 8
  • 43
  • 73
user1283776
  • 19,640
  • 49
  • 136
  • 276
  • did u check ubound(artAll) = 0 condition inside the for loop? – Ashok Raj Mar 26 '12 at 14:34
  • Do you mean the array hasn't been dimensionalized? If that is the case, then the only way to handle a `Ubound()` (or `LBound()`) error is with an error handler. – markblandford Mar 26 '12 at 14:38
  • What is a good way to handle this using the error handler? Should I catch a specific exception and use if err.number after the loop? Or is it better to goto a label? – user1283776 Mar 26 '12 at 14:41
  • 1
    You might use `If IsArray() Then` – iDevlop Mar 26 '12 at 14:43
  • possible duplicate of [How do I determine if an array is initialized in VB6?](http://stackoverflow.com/questions/183353/how-do-i-determine-if-an-array-is-initialized-in-vb6) – GSerg Mar 26 '12 at 15:54

5 Answers5

11
If Len(Join(artAll, "")) = 0 Then
     'your for loops here

Should work

Dan
  • 45,079
  • 17
  • 88
  • 157
7

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
assylias
  • 321,522
  • 82
  • 660
  • 783
2

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.

DavitosanX
  • 51
  • 4
  • 1
    Very nice! Interestingly If (myArray) Then doesn't work? Oh well... The IsEmpty() didn't work in my circumstance as I was working with an array of User Defined Types... but this solution did the trick! – Anthony Griggs Oct 11 '17 at 01:50
  • it looks good at first, but it did not work for me, because I may have run into the problems solved by @assylias function `isArrayEmpty(parArray as Variant)`: https://stackoverflow.com/a/9875956/1915920 – Andreas Covidiot Dec 21 '18 at 15:47
2

I like the solution given by @Dan but thought I would throw out there how I would normally handle an undimensionalized array:

Dim lngUboundTest As Long

lngUboundTest = -1
On Error Resume Next
lngUboundTest = UBound(artAll)
On Error GoTo 0

If lngUboundTest >= 0 Then
    'Your loop...
markblandford
  • 3,153
  • 3
  • 20
  • 28
0

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.

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:

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
TorontoJim
  • 11
  • 3