excel-vba Arrays Check if Array is Initialized (If it contains elements or not).

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

A common problem might be trying to iterate over Array which has no values in it. For example:

Dim myArray() As Integer
For i = 0 To UBound(myArray) 'Will result in a "Subscript Out of Range" error

To avoid this issue, and to check if an Array contains elements, use this oneliner:

If Not Not myArray Then MsgBox UBound(myArray) Else MsgBox "myArray not initialised"


Got any excel-vba Question?