Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

RE: string compare to array

916 views
Skip to first unread message

Gary''s Student

unread,
Mar 10, 2009, 3:46:02 PM3/10/09
to
How about something like:

Sub StringAlong()
sArray = Split("alpha,beta,gamma,delta,zeta,eta,theta", ",")
sample = "mm"
For i = LBound(sArray) To UBound(sArray)
If InStr(sArray(i), sample) > 0 Then
MsgBox ("found it")
Exit Sub
End If
Next
MsgBox ("did not find it")
End Sub

--
Gary''s Student - gsnu200837


"SteveDB1" wrote:

> Hello.
>
> Ok, I've been able to further reduce what I want to the following statement.
>
> I have a single string.
>
> I need to compare that string to an array of strings.
>
> If I find my single string in that array, I want to perform task A.
>
> Else, task B.
>
> So, my question is:
>
> 1- how do I populate that array so I have something for comparison?
>
> 2- I thought that I could use an if statement, with Like. Is this correct?
>
> Thank you.

Per Jessen

unread,
Mar 10, 2009, 3:54:42 PM3/10/09
to
Hi

Suppose you have strings to populate the array in A1:A10.

To compare the single string with the strings in the array, you have to loop
through the array.

See my example.

Sub aaa()
Dim MyArray(1 To 10) As String
Dim InArray As Boolean
Dim SingleString As String

SingleString = "Test"

For r = 1 To 10
MyArray(r) = Cells(r, 1).Value
Next

For r = 1 To 10
If SingleString = MyArray(r) Then
InArray = True
End If
Next

If InArray Then
msg = MsgBox(SingleString & " is found in MyArray")
End If

Regards,
Per

End Sub
"SteveDB1" <Stev...@discussions.microsoft.com> skrev i meddelelsen
news:2BB31666-4172-4D3E...@microsoft.com...

Rick Rothstein

unread,
Mar 10, 2009, 3:56:14 PM3/10/09
to
Here is another way to do it (without using a loop)...

Sub StringAlong()
sArray = Split("alpha,beta,gamma,delta,zeta,eta,theta", ",")

Sample = "mm"
If UBound(Filter(sArray, Sample)) >= 0 Then


MsgBox ("found it")

Else


MsgBox ("did not find it")

End If
End Sub

--
Rick (MVP - Excel)


"Gary''s Student" <GarysS...@discussions.microsoft.com> wrote in message
news:21AB1C7F-8ACC-448E...@microsoft.com...

0 new messages