#1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default Array

Is there a function in Visual Basic that returns the
number of entries in an array? I am writing a code that
loops through cells in excel, looks for a value -- if
there is a value it assigns that value to the next spot in
an array -- if the cell is blank the loop is broken. I
need to know how many values were found.

Thanks
Keri

  #2   Report Post  
Posted to microsoft.public.excel.programming
tod tod is offline
external usenet poster
 
Posts: 114
Default Array

This code may help you out.

HighNum = UBound(MyArray)

If your array is Base 0 (which is the default), then the
number of elements will be HighNum + 1. So if HighNum = 8,
then there are 9 elements. Some of those elements could be
blanks.

tod

-----Original Message-----
Is there a function in Visual Basic that returns the
number of entries in an array? I am writing a code that
loops through cells in excel, looks for a value -- if
there is a value it assigns that value to the next spot

in
an array -- if the cell is blank the loop is broken. I
need to know how many values were found.

Thanks
Keri

.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Array

UBound(ary)-LBound(Ary)+1

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Keri" wrote in message
...
Is there a function in Visual Basic that returns the
number of entries in an array? I am writing a code that
loops through cells in excel, looks for a value -- if
there is a value it assigns that value to the next spot in
an array -- if the cell is blank the loop is broken. I
need to know how many values were found.

Thanks
Keri



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 860
Default Array

Hi Keri,

Keri wrote:
Is there a function in Visual Basic that returns the
number of entries in an array? I am writing a code that
loops through cells in excel, looks for a value -- if
there is a value it assigns that value to the next spot in
an array -- if the cell is blank the loop is broken. I
need to know how many values were found.


The others' replies should answer your question. But I have a question for
you - if you have looped through the cells, shouldn't you already know the
number of elements you found? If not, you could simply add in an Integer or
Long counter variable that increments each time you find an element.

I'm not sure what range you're looping through, but depending on what you're
doing, it may be faster to get all the values in your range at once. For
example, if you want to get the values from A1 down column A until you hit
an empty cell, you could do something like this:

Sub test()
Dim vData As Variant

With Worksheets("Sheet1")
vData = .Range(.Cells(1, 1), .Cells(1, 1).End(xlDown)).Value
End With

MsgBox UBound(vData, 1)
End Sub

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

[please keep replies in the newsgroup - email address unmonitored]

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
Complex conditional summing - array COUNT works, array SUM gives#VALUE fatcatfan Excel Worksheet Functions 4 November 18th 09 06:41 PM
Prevent cell/array references from changing when altering/moving thecell/array nme Excel Discussion (Misc queries) 1 September 19th 08 01:53 PM
meaning of : IF(Switch; Average(array A, array B); array A) DXAT Excel Worksheet Functions 1 October 24th 06 06:11 PM
combining cells and array from different sheets into an array to pass to IRR() [email protected] Excel Discussion (Misc queries) 3 September 11th 06 07:17 AM
variant array containing cel adresses convert to actual ranges-array Peter[_21_] Excel Programming 5 December 10th 03 09:50 PM


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

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

About Us

"It's about Microsoft Excel"