Counting items in an array

  • Thread starter Thread starter ajitpalsingh200
  • Start date Start date
A

ajitpalsingh200

How do i count the number of items in array

lets say i have declaration as below

DiscreteData = Array("MPN", "Vendor")

How do i count the items?

thankx in adavanc
 
ajitpalsingh200,

Dim ArrayItemCount as Long
ArrayItemCount =UBound(DiscreteData)-LBound(DiscreteData)

NickHK
 
NickHK said:
ajitpalsingh200,

Dim ArrayItemCount as Long
ArrayItemCount =UBound(DiscreteData)-LBound(DiscreteData)

NickHK

The above works for a 1-based array; to allow for a 0-based or 1-based
array, substitute
ArrayItemCount =UBound(DiscreteData)-LBound(DiscreteData) + 1

Alan Beban
 
There is no built-in support for finding the dimensions of an array in VBA/VB
without prior knowledge of its rank (number of dimensions). The nearest
solution is to do it by trial and error.
 
I have an example of how to determine number of dimensions in array on my
website "Number of Dimensions"

My routine trials until it errors - just as you say.
 
If you simply want the count of the number of elements in an array,
irrespection of the humber of dimensions it has, try:

Sub xy()

For Each ele In xx
cnt = cnt + 1
Next
End Sub
 
If you simply want the count of the number of elements in an array,
irrespection of the humber of dimensions it has, try:

Sub xy()
cnt = 0
For Each ele In YourArray
cnt = cnt + 1
Next
End Sub
 

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