Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc,microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Number of Items in an Array.

Hi,

How do I find out the number of items in an array, so that I can loop check
the values in the array with the incoming data.

Dim Users as Variant
Dim NumValues, Counter as Integer
Users = Array("Bob","Fred")

NumValues = Users

For Counter = 0 to NumValues
CheckUser = Users(Counter)
If CheckUser = IncomingUser then
Print "Cool" - okay this is just pseudo code.
Else
Print "Not Cool"
End If
next

Cheers
Craig.


  #2   Report Post  
Posted to microsoft.public.excel.misc,microsoft.public.excel.programming
external usenet poster
 
Posts: 3,885
Default Number of Items in an Array.

Hi
NumValues = ubound(Users)

--
Regards
Frank Kabel
Frankfurt, Germany


Craig & Co. wrote:
Hi,

How do I find out the number of items in an array, so that I can loop
check the values in the array with the incoming data.

Dim Users as Variant
Dim NumValues, Counter as Integer
Users = Array("Bob","Fred")

NumValues = Users

For Counter = 0 to NumValues
CheckUser = Users(Counter)
If CheckUser = IncomingUser then
Print "Cool" - okay this is just pseudo code.
Else
Print "Not Cool"
End If
next

Cheers
Craig.


  #3   Report Post  
Posted to microsoft.public.excel.misc,microsoft.public.excel.programming
external usenet poster
 
Posts: 49
Default Number of Items in an Array.

A small correction, you need to add 1 as the first Item is 0:

NumValues = UBound(Users) + 1

KL

"Frank Kabel" wrote in message
...
Hi
NumValues = ubound(Users)

--
Regards
Frank Kabel
Frankfurt, Germany


Craig & Co. wrote:
Hi,

How do I find out the number of items in an array, so that I can loop
check the values in the array with the incoming data.

Dim Users as Variant
Dim NumValues, Counter as Integer
Users = Array("Bob","Fred")

NumValues = Users

For Counter = 0 to NumValues
CheckUser = Users(Counter)
If CheckUser = IncomingUser then
Print "Cool" - okay this is just pseudo code.
Else
Print "Not Cool"
End If
next

Cheers
Craig.




  #4   Report Post  
Posted to microsoft.public.excel.misc,microsoft.public.excel.programming
external usenet poster
 
Posts: 109
Default Number of Items in an Array.

Hi Craig

NumValues = Ubound(Users) - (Lbound(Users)=0)

The reason for subtracting is, that TRUE in VBA is -1,
so if Lbound(Users) is 0, 1 is added ( -(-1) = +1) to
Ubound(Users).
In contrast, TRUE in Excel is 1 (positive one)

Normally you would loop the array with:

For Counter = Lbound(Users) to Ubound(Users)
etc.

Please notice, that

Dim NumValues, Counter as Integer

will dimension NumValues as Variant. Each variable
must be dimensioned explicitly:

Dim NumValues as Integer, Counter as Integer


--
Best Regards
Leo Heuser

Followup to newsgroup only please.

"Craig & Co." skrev i en meddelelse
...
Hi,

How do I find out the number of items in an array, so that I can loop

check
the values in the array with the incoming data.

Dim Users as Variant
Dim NumValues, Counter as Integer
Users = Array("Bob","Fred")

NumValues = Users

For Counter = 0 to NumValues
CheckUser = Users(Counter)
If CheckUser = IncomingUser then
Print "Cool" - okay this is just pseudo code.
Else
Print "Not Cool"
End If
next

Cheers
Craig.




  #5   Report Post  
Posted to microsoft.public.excel.misc,microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Number of Items in an Array.

That depends upon the array lower bound. To be absolutely sure, you could
use

For Counter = LBound(Users,1) To UBound(Users,1)

caters for most options

--

HTH

RP

"KL" wrote in message
...
A small correction, you need to add 1 as the first Item is 0:

NumValues = UBound(Users) + 1

KL

"Frank Kabel" wrote in message
...
Hi
NumValues = ubound(Users)

--
Regards
Frank Kabel
Frankfurt, Germany


Craig & Co. wrote:
Hi,

How do I find out the number of items in an array, so that I can loop
check the values in the array with the incoming data.

Dim Users as Variant
Dim NumValues, Counter as Integer
Users = Array("Bob","Fred")

NumValues = Users

For Counter = 0 to NumValues
CheckUser = Users(Counter)
If CheckUser = IncomingUser then
Print "Cool" - okay this is just pseudo code.
Else
Print "Not Cool"
End If
next

Cheers
Craig.








  #6   Report Post  
Posted to microsoft.public.excel.misc,microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Number of Items in an Array.

since arrays are not limited to 0 or 1 for a lower bound it might be better
to use the old tried and true

Ubound(users) - Lbound(users) + 1

Sub Tester2()
Dim Users(6 To 12)
Debug.Print UBound(Users) - (LBound(Users) = 0)
Debug.Print UBound(Users) - LBound(Users) + 1
End Sub

Produced:
12
7
--
Regards,
Tom Ogilvy

"Leo Heuser" wrote in message
...
Hi Craig

NumValues = Ubound(Users) - (Lbound(Users)=0)

The reason for subtracting is, that TRUE in VBA is -1,
so if Lbound(Users) is 0, 1 is added ( -(-1) = +1) to
Ubound(Users).
In contrast, TRUE in Excel is 1 (positive one)

Normally you would loop the array with:

For Counter = Lbound(Users) to Ubound(Users)
etc.

Please notice, that

Dim NumValues, Counter as Integer

will dimension NumValues as Variant. Each variable
must be dimensioned explicitly:

Dim NumValues as Integer, Counter as Integer


--
Best Regards
Leo Heuser

Followup to newsgroup only please.

"Craig & Co." skrev i en meddelelse
...
Hi,

How do I find out the number of items in an array, so that I can loop

check
the values in the array with the incoming data.

Dim Users as Variant
Dim NumValues, Counter as Integer
Users = Array("Bob","Fred")

NumValues = Users

For Counter = 0 to NumValues
CheckUser = Users(Counter)
If CheckUser = IncomingUser then
Print "Cool" - okay this is just pseudo code.
Else
Print "Not Cool"
End If
next

Cheers
Craig.






  #7   Report Post  
Posted to microsoft.public.excel.misc,microsoft.public.excel.programming
external usenet poster
 
Posts: 109
Default Number of Items in an Array.

I gave my answer, because the OP's example used the
Array function, but of course you're correct, that
Ubound(users) - Lbound(users) + 1
is the general way of getting the number of elements in an array.
Most of the time, I use it myself :-)

--
Best Regards
Leo Heuser

"Tom Ogilvy" skrev i en meddelelse
...
since arrays are not limited to 0 or 1 for a lower bound it might be

better
to use the old tried and true

Ubound(users) - Lbound(users) + 1

Sub Tester2()
Dim Users(6 To 12)
Debug.Print UBound(Users) - (LBound(Users) = 0)
Debug.Print UBound(Users) - LBound(Users) + 1
End Sub

Produced:
12
7
--
Regards,
Tom Ogilvy

"Leo Heuser" wrote in message
...
Hi Craig

NumValues = Ubound(Users) - (Lbound(Users)=0)

The reason for subtracting is, that TRUE in VBA is -1,
so if Lbound(Users) is 0, 1 is added ( -(-1) = +1) to
Ubound(Users).
In contrast, TRUE in Excel is 1 (positive one)

Normally you would loop the array with:

For Counter = Lbound(Users) to Ubound(Users)
etc.

Please notice, that

Dim NumValues, Counter as Integer

will dimension NumValues as Variant. Each variable
must be dimensioned explicitly:

Dim NumValues as Integer, Counter as Integer


--
Best Regards
Leo Heuser

Followup to newsgroup only please.

"Craig & Co." skrev i en meddelelse
...
Hi,

How do I find out the number of items in an array, so that I can loop

check
the values in the array with the incoming data.

Dim Users as Variant
Dim NumValues, Counter as Integer
Users = Array("Bob","Fred")

NumValues = Users

For Counter = 0 to NumValues
CheckUser = Users(Counter)
If CheckUser = IncomingUser then
Print "Cool" - okay this is just pseudo code.
Else
Print "Not Cool"
End If
next

Cheers
Craig.








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
transpose 1 column of 100 items to 10 x 10 array kidsDad Excel Discussion (Misc queries) 2 September 18th 09 10:04 PM
Number of items in a set Francois[_2_] Excel Worksheet Functions 9 November 20th 08 06:57 AM
need formula to count non-zero items in an array based on a vlooku BKP Excel Worksheet Functions 2 September 17th 08 08:55 PM
How to Count the number of "rows" (or Array items) included in a Sumif formula? EagleOne Excel Discussion (Misc queries) 3 July 30th 07 06:25 PM
create an array with unique items IN MEMORY Werner Rohrmoser Excel Worksheet Functions 1 September 25th 05 02:55 PM


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

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"