For a variant array, you could use something like:
Dim lngCount As Long
Dim lngIndex As Long
lngCount = 0
For lngIndex = LBound(arrMyArray) To UBound(arrArray)
If Not IsEmpty(arrMyarray(lngIndex)) Then lngCount = lngCount + 1
Next lngIndex
MsgBox lngCount
where arrMyArray is the name of the array (assumed to be one-dimensional). IsEmpty works only for variants, because each array element is assigned a default value when the array is created.For an array of:
· Variants, this default value is Empty.
· Integers or another numeric type, it is 0.
· Strings, it is "".
· Booleans, it is False.
If you have an array of Integers, Strings or Booleans, the array elements aren't ever truly "empty". Taking an integer array, for example, you can't distinguish between an element that is 0 because it was never assigned a value, and an element that is 0 because it was set to 0 explicitly.
Redimming, as Skip suggests, is one way of making sure arrays of Integers, Strings and Booleans only hold meaningful elements. The trick, though, is to remove elements that are no longer is use - in the correct order. Unfortunately, you can't just pop an element out of the middle of the array - you'd need to shuffle all the other elements down (adjusting any pointers), then redim away the top-most element.
Cheers