View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Chip Pearson Chip Pearson is offline
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