Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
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
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
"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 |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Use formulas for array elements | Excel Discussion (Misc queries) | |||
elements in an array | Excel Programming | |||
Is there a way to count how many elements there are in an array? | Excel Programming | |||
Reference Elements w/in an Array | Excel Discussion (Misc queries) | |||
Shifting Array Elements | Excel Programming |