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

Hey

How do you test to see if a particular value is in a
collection object?

For example, I have a collection with 5 items in it. The
items in it a "abc", "def", "ghi", "jkl", and "mno".
How can I test to see if the value "jkl" is in this
collection, and return true or false?


Thanks
Todd
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,163
Default Collection

Function CollectionContains(TxtValue as String) as Boolean
Dim MyObject as Object
CollectionContains = False
For each MyObject in ObjectName.CollectionName ' You need to put the actual
object and collection name in the code here
If MyObject.Value = TxtValue Then CollectionContains = True
Next MyObject
End Function

"Todd Huttenstine" wrote:

Hey

How do you test to see if a particular value is in a
collection object?

For example, I have a collection with 5 items in it. The
items in it a "abc", "def", "ghi", "jkl", and "mno".
How can I test to see if the value "jkl" is in this
collection, and return true or false?


Thanks
Todd

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 783
Default Collection

Todd Huttenstine wrote:
Hey

How do you test to see if a particular value is in a
collection object?

For example, I have a collection with 5 items in it. The
items in it a "abc", "def", "ghi", "jkl", and "mno".
How can I test to see if the value "jkl" is in this
collection, and return true or false?


Thanks
Todd


If you check the reference to Microsoft Scripting Runtime in the VB
Editor, you can use a Dictionary Object instead of a Collection and it
gets easy.

Dim dict As New Dictionary
Dim arr, Elem
arr = Array("abc", "def", "ghi", "jkl", "mno")
For Each Elem In arr
dict.Add CStr(Elem), Elem
Next
Debug.Print dict.Exists("jkl") '<--returns True
Debug.Print dict.Exists("xyz") '<--returns False

Alan Beban
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 783
Default Collection

I should have mentioned that this assumes that the elements in the array
are unique.

Alan Beban

Alan Beban wrote:
Todd Huttenstine wrote:

Hey

How do you test to see if a particular value is in a collection object?

For example, I have a collection with 5 items in it. The items in it
a "abc", "def", "ghi", "jkl", and "mno". How can I test to see if
the value "jkl" is in this collection, and return true or false?


Thanks
Todd



If you check the reference to Microsoft Scripting Runtime in the VB
Editor, you can use a Dictionary Object instead of a Collection and it
gets easy.

Dim dict As New Dictionary
Dim arr, Elem
arr = Array("abc", "def", "ghi", "jkl", "mno")
For Each Elem In arr
dict.Add CStr(Elem), Elem
Next
Debug.Print dict.Exists("jkl") '<--returns True
Debug.Print dict.Exists("xyz") '<--returns False

Alan Beban

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

Todd,

Here is a function to test it

Public Function IfExists(col As Collection, ByVal sKey As String)

On Error GoTo NoSuchKey

If VarType(col.Item(sKey)) = vbObject Then
' force an error condition if key does not exist
End If

IfExists = True

Exit Function

NoSuchKey:
IfExists = False

End Function





--

HTH

RP
(remove nothere from the email address if mailing direct)


"Todd Huttenstine" wrote in message
...
Hey

How do you test to see if a particular value is in a
collection object?

For example, I have a collection with 5 items in it. The
items in it a "abc", "def", "ghi", "jkl", and "mno".
How can I test to see if the value "jkl" is in this
collection, and return true or false?


Thanks
Todd



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
help with data collection! brandon roland[_2_] New Users to Excel 1 August 8th 07 10:09 PM
Does anybody have a CD collection template PeterM Excel Discussion (Misc queries) 2 November 27th 04 05:46 PM
Reset New Collection Tony Di Stasi[_2_] Excel Programming 2 February 19th 04 03:21 PM
2 Collection questions Stuart[_5_] Excel Programming 5 January 30th 04 04:50 PM
Names Collection MarkC[_2_] Excel Programming 2 January 28th 04 10:47 AM


All times are GMT +1. The time now is 11:50 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"