Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search for text in a Variable 2

Status
Not open for further replies.

mark01

Technical User
Jan 17, 2001
600
US
I have the following code...

TextVar = "This is just a sentence."

How can I search for a word in that variable, and if it is found, display the whole sentence.

For example, I'd like to search for the word "just" inside TextVar, and if it is present, then MsgBox TextVar...

Anyone know how to do this?
 
Code:
If InStr(1, TextVar, "Just", vbTextCompare) > 0 Then
    MsgBox Var
Else
    MsgBox "Search string not found"
End If

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
If Instr ( TextVar, "just" ) > 0 Then MsgBox TextVar
 
Thank you very much, that did what I wanted it to do...

Another question... It will only find it if the "just" is all lowercase. Can I have it not depend on capitalization?

For example, I would like to search for "just" & "Just" and get the same thing....
 
Follow my example above. The vbTextCompare part specifies that the search will NOT be case sensitive. Using vbBinaryCompare will cause the search to be case sensitive.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Just a side note...
The > 0 part is not needed...

Anytime something results in non Zero, it is considered True, adding > 0 just doubles the effort...
Code:
If InStr(1, TextVar, "Just", vbTextCompare) Then
    MsgBox Var
Else
    MsgBox "Search string not found"
End If

Another Option... would be to use the Like Keyword...
Code:
If TextVar Like "*Just*" Then
  ...
Else
  ...
End If

*Note: Again, to avoid case issues, the default is Binary Compare, to use Text Compare, place:
Code:
Option Compare Text
At the top of your module (where Option Explicit goes)
This will also correct the behavior of instr(), so that both gmmastros's and Golom's will work the same...

Here is a list of Patterns you can use, in the right side operand of the operator: (Text Like Pattern)
Code:
[b]Kind of match          Pattern     Match (returns True)    No match (returns False)[/b]
Multiple characters    a*a         aa, aBa, aBBBa          aBC
                       *ab*        abc, AABB, Xab          aZb, bac
Special character      a[*]a       a*a                     aaa
Multiple characters    ab*         abcdefg, abc            cab, aab
Single character       a?a         aaa, a3a, aBa           aBBBa
Single digit           a#a         a0a, a1a, a2a           aaa, a10a
Range of characters    [a-z]       f, p, j                 2, &
Outside a range        [!a-z]      9, &, %                 b, a
Not a digit            [!0-9]      A, a, &, ~              0, 1, 9
Combined               a[!b-m]#    An9, az0, a99           abc, aj0

A third option would be to use regular expressions, but that would probably be an overkill, though we can provide example if you are interested...
(Or just do a keyword search for: Reg Exp)

Hope This Helps ;-)

Visit My Site
PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top