Is there a way to count how many elements there are in an array?

  • Thread starter Thread starter Peter
  • Start date Start date
P

Peter

But not by doing an for i = lbound(x()) to ubound(x()) and counter?

Thanks

Peter
 
But not by doing an for i = lbound(x()) to ubound(x()) and counter?

Thanks

Peter

Dim lngElements as Long
lngElements = WorksheetFunction.CountA(x)

will tell you the number of elements in the x array that have been
assigned a value.

Ken Johnson
 
Here is one way, assuming option base zero.

Sub arry()
Dim myArry As Variant
myArry = Array("This", "That", "Other")
MsgBox UBound(myArry) + 1
End Sub
 
If the array (say arr1) is a one-dimensional array or a single column:

no. of elements = Ubound(arr1) - Lbound(arr1) + 1

If it's a 2 dimensional array:

no. of elements = (Ubound(arr1) - Lbound(arr1) + 1) * _
(Ubound(arr1,2) - Lbound(arr1,2) + 1

Alan Beban
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top