+ Reply to Thread
Results 1 to 4 of 4

search for multiple words in a string

Hybrid View

  1. #1
    Registered User
    Join Date
    10-18-2011
    Location
    KamoteLand
    MS-Off Ver
    Excel 2007
    Posts
    6

    search for multiple words in a string

    I was trying to search at least two words in a string then execute a code. For example, I have

    string = "CD_HJ_TY_AB"

    Here's what I like to accomplish:
    1. Find AB and CD
    2. If found, do something. Else do nothing.

    I tried to use Instring() and AND but I can't get it working.
    Any suggestions?
    Last edited by pcbpinoy; 10-28-2011 at 04:29 AM. Reason: solved..

  2. #2
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: search for multiple words in a string

    For example:
    Sub pcbpinoy()
    Dim s$
    s = "CD_HJ_TY_AB"
    If InStr(s, "AB") Then MsgBox "Found AB"
    If InStr(s, "AB") And InStr(s, "CD") Then MsgBox "Found AB and CD"
    End Sub

  3. #3
    Registered User
    Join Date
    10-18-2011
    Location
    KamoteLand
    MS-Off Ver
    Excel 2007
    Posts
    6

    Re: search for multiple words in a string

    thanks a lot.. i was doing

    s = "CD_HJ_TY_AB"
    If (InStr(s, "AB") =1) And (InStr(s, "CD")=1)

  4. #4
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: search for multiple words in a string

    pcbpinoy, then, perhaps, would be correct this:
    If InStr(s, "AB") > 0 And InStr(s, "CD") > 0 Then
    instead of
    If (InStr(s, "AB") =1) And (InStr(s, "CD")=1) Then

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1