elements in an array

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hi
how do i know whether a particular array contains any elements?
thanks
 
hemu,

Try code like this:

Dim myArr() As Integer

On Error GoTo NotSet
MsgBox "myArr has " & UBound(myArr) - LBound(myArr) + 1 & " elements"
Exit Sub
NotSet:
MsgBox "myArr is not yet set"
ReDim myArr(0 To 100)
MsgBox "myArr now has " & UBound(myArr) - LBound(myArr) + 1 & " elements"

HTH,
Bernie
MS Excel MVP
 
thanks bernie
i was looking for a function like alen(myarray,1) we have in
in FoxPro
thanks again guys
 
Hemant_india said:
thanks bernie
i was looking for a function like alen(myarray,1) we have in
in FoxPro
thanks again guys

What does alen(myarray,1) return in FoxPro?

Alan Beban
 
The following function will return the number of elements in the first
dimension of the array Arr or 0 if Arr is a dynamic array that has not yet
been allocated or has been cleared with Erase, or -1 is Arr is not an array.


Function NumElements(Arr As Variant) As Long
Dim LB As Long
If IsArray(Arr) = False Then
NumElements = -1
Exit Function
End If
On Error Resume Next
If Not IsError(LBound(Arr)) And LBound(Arr) < UBound(Arr) Then
NumElements = UBound(Arr) - LBound(Arr) + 1
Else
NumElements = 0
End If
End Function

Usage Example:

Dim N As Long
Dim V As Variant ' returns -1
' Dim V(1 To 10) ' returns 10
' Dim V() ' returns 0
' ReDim V(1 To 10) ' returns 10
'
N = NumElements(V)
Select Case N
Case -1
Debug.Print "Arr is not an array."
Case 0
Debug.Print "Arr has not been allocated"
Case Else
Debug.Print "Arr has " & CStr(N) & " elements"
End Select




--
Cordially,
Chip Pearson
Microsoft MVP - Excel, 10 Years
Pearson Software Consulting
www.cpearson.com
(email on the web site)
 
Chip said:
The following function will return the number of elements in the first
dimension of the array Arr or 0 if Arr is a dynamic array that has not
yet been allocated or has been cleared with Erase, or -1 if Arr is not
an array.


Function NumElements(Arr As Variant) As Long
Dim LB As Long
If IsArray(Arr) = False Then

Suggest

If Not TypeName(Arr) Like "*()" Then

instead of the last line above. Otherwise, if Arr is a multi-celled
range, the function will return 0 rather than -1

BTW, I still don't know whether the OP was referring to the number of
elements or the number of non-empty elements, since I don't know what
alen(myarray,1) returns in FoxPro.

Alan Beban
 
Alan Beban said:
Suggest

If Not TypeName(Arr) Like "*()" Then

That's a good point, but I don't consider ranges to be arrays, regardless of
what IsArray has to say about the matter. That said, your suggestion is
probably the better way to go.
elements or the number of non-empty elements,

I don't think there is a way to determine this. For example, an element in
an array of Longs may have the value 0. This might be the result of code
explicitly setting the element's value to 0, in which case it would be
considered a used element of the array, or it might be the default value of
the element that was never set, in which case is would be considered an
unused element of the array. With standard VBA arrays, I don't think there
is any way to discern the difference. Perhaps one could initialize the array
of Variants with all Nulls and assume that the code populating the array
would never assign Null to an array element. On the other hand, FoxPro,
about which I know nothing, may have its own flavor of arrays that are
different from standard VBA arrays.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel, 10 Years
Pearson Software Consulting
www.cpearson.com
(email on the web site)
 

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