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


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"