ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Is there a way to count how many elements there are in an array? (https://www.excelbanter.com/excel-programming/396006-there-way-count-how-many-elements-there-array.html)

Peter[_61_]

Is there a way to count how many elements there are in an array?
 
But not by doing an for i = lbound(x()) to ubound(x()) and counter?

Thanks

Peter


Ken Johnson

Is there a way to count how many elements there are in an array?
 
On Aug 22, 12:01 am, Peter wrote:
But not by doing an for i = lbound(x()) to ubound(x()) and counter?

Thanks

Peter


Dim lngElements as Long
lngElements = WorksheetFunction.CountA(x)

will tell you the number of elements in the x array that have been
assigned a value.

Ken Johnson


JLGWhiz

Is there a way to count how many elements there are in an array?
 
Here is one way, assuming option base zero.

Sub arry()
Dim myArry As Variant
myArry = Array("This", "That", "Other")
MsgBox UBound(myArry) + 1
End Sub


"Peter" wrote:

But not by doing an for i = lbound(x()) to ubound(x()) and counter?

Thanks

Peter



Alan Beban[_2_]

Is there a way to count how many elements there are in an array?
 
If the array (say arr1) is a one-dimensional array or a single column:

no. of elements = Ubound(arr1) - Lbound(arr1) + 1

If it's a 2 dimensional array:

no. of elements = (Ubound(arr1) - Lbound(arr1) + 1) * _
(Ubound(arr1,2) - Lbound(arr1,2) + 1

Alan Beban

Peter wrote:
But not by doing an for i = lbound(x()) to ubound(x()) and counter?

Thanks

Peter



All times are GMT +1. The time now is 05:12 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com