Good way to handle empty arrays?

OldManExcellor

New Member
Joined
May 30, 2011
Messages
45
I have a dynamic array that is somethimes empty. I want to do an operation on all array elements in my code. When there are no array elements, I want the code to just continue.

But the two methods to loop array elements that I know of aren't skipped when the dynamic array is empty. The generate errors.

I have been able to handle this using OnError Resume Next. But I'm thinking maybe there is some smoother way to avoid getting errors when the dynamic array is empty?

Your help is very appreciated.

Code:
'Method 1
    For i = 0 To arrayCount
        Do stuff...
    Next i

'Method 2
    For Each currentElement In family
        Do stuff...
    Next
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Hi OldMan,


Take a look here:

http://www.cpearson.com/excel/VBAArrays.htm

Download the module and then add it to your workbook.

You will then be able to use the IsArrayEmpty function in an if statement to determine if the array is empty before calaculating it, thus solving the problem of your empty arrays. There are also a lot of other useful array based commands.
 
Upvote 0

Forum statistics

Threads
1,224,584
Messages
6,179,693
Members
452,938
Latest member
babeneker

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top