Maybe this is the simplest thing ever but i'm a beginner trying to learn at least 1 language.
How come the Script below keeps displaying the Else message until it finds the string? I was expecting for it to display 1 single Box whether it was found or not? why does it keep itterating through the Else condition?

Dim myArray,myString,myString2,intCount

myString = "my;string;question;hope;you;can;answer"
myString2 = "question"

myArray = split(myString,";")

For Each intCount In myArray
If intCount = myString2 Then
msgBox("String Found!!")
Else
msgBox("Sorry, String Not Found")
End If
Next

Recommended Answers

All 4 Replies

Maybe this is the simplest thing ever but i'm a beginner trying to learn at least 1 language.
How come the Script below keeps displaying the Else message until it finds the string? I was expecting for it to display 1 single Box whether it was found or not? why does it keep itterating through the Else condition?

Dim myArray,myString,myString2,intCount

myString = "my;string;question;hope;you;can;answer"
myString2 = "question"

myArray = split(myString,";")

For Each intCount In myArray
If intCount = myString2 Then
msgBox("String Found!!")
Else
msgBox("Sorry, String Not Found")
End If
Next

I am totally expecting it to behave like that. The process will go like this.

  1. Compare intCount, which is "my" to "question". It is not equal, then show "Sorry, String Not Found".
  2. Compare intCount, which is "string" to "question". It is not equal, then show "Sorry, String Not Found".
  3. Compare intCount, which is "question" to "question". It is equal, then show "String Found!!".

The process will go on until you loop through all the element in myArray.

Ok, Thanks, So what would be the correct way for it to only show "String Found!!" or "Not Found" only Once?

You can use variable to check whether you can find matched string or not in your search process.

Dim isFound As Boolean = False

For Each intCount In myArray
   If intCount = myString2 Then
      isFound = True
      Exit For
   End If
Next

If isFound Then
   Msgbox("Found.")
Else
   Msgbox("Not found.")
End If

Hey,
Thanks a lot, that worked! Thanks for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.