+ Reply to Thread
Results 1 to 5 of 5

Find Text within Text String

  1. #1
    Registered User
    Join Date
    10-11-2004
    Posts
    18

    Find Text within Text String

    Hi All,

    I've got bit of a puzzle here. I've got 2 text strings for example A = "Model A" and B = "A". Now I want to, in VBA, find Text B within Text A. How do I do that?

    Thanks

    Lenny

  2. #2
    Registered User
    Join Date
    08-24-2005
    Location
    Philippines
    Posts
    75
    Hi Lenny! To find a substring w/in a string, you use the InsStr function:

    InStr([start, ]string1, string2[, compare])

    InStr(1, A, B)

    It returns the position of string2 in string1 if found.

    See VBA help for more details.

    Quote Originally Posted by Lenny_821
    Hi All,

    I've got bit of a puzzle here. I've got 2 text strings for example A = "Model A" and B = "A". Now I want to, in VBA, find Text B within Text A. How do I do that?

    Thanks

    Lenny

  3. #3
    Registered User
    Join Date
    10-11-2004
    Posts
    18
    Thanks T-容x

    Lenny

  4. #4
    Stefi
    Guest

    RE: Find Text within Text String

    In your example InStr(A,B) gives 7,
    because "A" is the seventh character of "Model A" .
    Regards,
    Stefi


    Lenny_821 ezt *rta:

    >
    > Hi All,
    >
    > I've got bit of a puzzle here. I've got 2 text strings for example A =
    > "Model A" and B = "A". Now I want to, in VBA, find Text B within Text
    > A. How do I do that?
    >
    > Thanks
    >
    > Lenny
    >
    >
    > --
    > Lenny_821
    > ------------------------------------------------------------------------
    > Lenny_821's Profile: http://www.excelforum.com/member.php...o&userid=15179
    > View this thread: http://www.excelforum.com/showthread...hreadid=400363
    >
    >


  5. #5
    Norman Jones
    Guest

    Re: Find Text within Text String

    Hi Lenny_821,

    Look at the InStr function in VBA help.

    Assume:
    A1: Model A
    B1: A

    Sub Tester01()
    Dim str1 As String, str2 As String

    str1 = Range("A1").Value
    str2 = Range("B1").Value

    If InStr(1, str1, str2, vbTextCompare) > 0 Then
    'substring found, do something, e.g.
    MsgBox """" & str2 & """ found in " & """" & str1 & """"
    'found in " & "" & str1 & """"

    Else
    'substring not found, do something else, e.g.
    MsgBox """" & str2 & """ not found in " & """" & str1 & """"
    End If
    End Sub


    ---
    Regards,
    Norman



    "Lenny_821" <[email protected]> wrote
    in message news:[email protected]...
    >
    > Hi All,
    >
    > I've got bit of a puzzle here. I've got 2 text strings for example A =
    > "Model A" and B = "A". Now I want to, in VBA, find Text B within Text
    > A. How do I do that?
    >
    > Thanks
    >
    > Lenny
    >
    >
    > --
    > Lenny_821
    > ------------------------------------------------------------------------
    > Lenny_821's Profile:
    > http://www.excelforum.com/member.php...o&userid=15179
    > View this thread: http://www.excelforum.com/showthread...hreadid=400363
    >




+ 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