x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 185

# Find something in a collection

Forexample, I have a collection C = [1,2,3,45,32,21,7,9] and I have a variable A. I want to check if the value of A is a member of collection C. How do I do that in VB? Thanks..
0
veraps
1 Solution

Commented:
a collection of what?
0

Commented:
Dim TestItem As Variant

For each TestItem in  C
If TestItem = A Then MsgBox("Here it is !")
Next Test

Thats was it
Best regards ;O)
WoK
0

Commented:
Hi veraps,

Dim C() As Integer, A As Integer, i As Integer
Dim IsFound As Boolean

Redim Preserve C(7)

IsFound = False

c(0)=1
c(1)=2
c(2)=3
c(3)=45
c(4)=32
c(5)=21
c(6)=7
c(7)=9

For i = Lbound(C) to UBound(C)
If A = C(i) Then IsFound = True:Exit For
Next i

'Hope will help.

0

Commented:
Sorry:

Next TestItem

was right ...

_________________________________________________________
Here is a complete example:

Dim TestItem As Variant
Dim C As Collection
Dim A As Integer

A = 3
Set C = New Collection

For Each TestItem In C
If TestItem = A Then MsgBox ("Item found !")
Next TestItem

End Sub
0

Commented:
Hi,

Try this:

Private Sub Command1_Click()
Dim c As New Collection, i As Integer, a As Integer, aMember
For i = 1 To 8
Next i
a = 4
For Each aMember In c
If aMember = a Then
MsgBox "a is a member in collection c."
End If
Next
End Sub
0

Commented:
You would add an item to a collection.

Then when you are doing a look-up for your variable, look up via the key.

msgbox collection.item("a") - will return what the item is for that key.

If it cannot find it, then have some sort of error routine that it wasn't found.

0

Commented:
if you only use one collection in your app you can replace it with a dictionary ( make a reference to MS scripting runtime)

dim c as new dictionary

there is allready a function itemexits and you don't have to search by a for next loop
0

Commented:
Hi,

If InStr(1, Text1.Text, "45", vbTextCompare) Or _
InStr(1, Text1.Text, "32", vbTextCompare) Then...

Enjoy
0

Commented:
Try this:

For lX = 1 to colC.Count
if colC(lx) = varA then
msgbox varA & " found at position " & lx & " of colX."
exit For
end if
next lX
0

Commented:
Hi veraps,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

Accept WolfgangKoenig's comment(s) as an answer.

veraps, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
0

Commented:
Per recommendation, force-accepted.

Netminder
CS Moderator
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.