Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default elements in an array

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)


"Hemant_india" wrote in message
...
hi
how do i know whether a particular array contains any elements?
thanks
--
hemu


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 783
Default elements in an array

Chip Pearson wrote:
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
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default elements in an array

"Alan Beban" wrote in message

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)

"Alan Beban" wrote in message
...
Chip Pearson wrote:
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


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Use formulas for array elements hmm Excel Discussion (Misc queries) 3 December 3rd 07 01:15 PM
elements in an array Alan Beban[_2_] Excel Programming 0 November 21st 07 08:21 PM
Is there a way to count how many elements there are in an array? Peter[_61_] Excel Programming 3 August 24th 07 03:54 AM
Reference Elements w/in an Array M Moore Excel Discussion (Misc queries) 2 October 16th 06 03:33 PM
Shifting Array Elements Trip[_3_] Excel Programming 6 July 30th 05 07:30 PM


All times are GMT +1. The time now is 08:11 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"