LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #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


 
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 02:10 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"