+ Reply to Thread
Results 1 to 7 of 7

Find the position of a substring in a string

Hybrid View

  1. #1
    Registered User
    Join Date
    06-08-2012
    Location
    Bucharest, Romania
    MS-Off Ver
    Excel 2003
    Posts
    50

    Find the position of a substring in a string

    Hello guys,

    I have a long string with different (and unique) names of people. In this string I am interested to find the position of a specific substring not by characters but by words...Let say my string has 1000 words and I am looking for the "Jeremy Wade" word position. Suppose this index is 705 (I have 704 words before).
    Is it possible to find this index without any loop thru each value of this string ?
    Dim myString as String
    myString = "John Scott Mary Fox Terry Dyke......Jeremy Wade......"
    Thank you in advance.

  2. #2
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Find the position of a substring in a string

    Try Instr()
    If the string has no match then zero is returned, otherwise it returns the position of the first match found, 37 in this example.
    myString = "John Scott Mary Fox Terry Dyke......Jeremy Wade......"
    posn = instr(mystring,"Jeremy Wade")
    If you need any more information, please feel free to ask.

    However,If this takes care of your needs, please select Thread Tools from menu above and set this topic to SOLVED. It helps everybody! ....

    Also
    اس کی مدد کرتا ہے اگر
    شکریہ کہنے کے لئے سٹار کلک کریں
    If you are satisfied by any members response to your problem please consider using the small Star icon bottom left of their post to show your appreciation.

  3. #3
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Find the position of a substring in a string

    Hi,

    In your example is Jeremy Wade the 7th word, or are you looking for the result of 4? i.e. The 4th name.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  4. #4
    Registered User
    Join Date
    06-08-2012
    Location
    Bucharest, Romania
    MS-Off Ver
    Excel 2003
    Posts
    50

    Re: Find the position of a substring in a string

    Hi Richard,

    Jeremy Wade is the 705 th word....So, I am interested to find the position of the word in the entire string. Do you think is it possible? Thanks all.

  5. #5
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Find the position of a substring in a string

    Quote Originally Posted by nemo66ro View Post
    Hi Richard,

    Jeremy Wade is the 705 th word....So, I am interested to find the position of the word in the entire string. Do you think is it possible? Thanks all.
    Hi,

    That isn't what I was asking. I said what's the position in your 'example' string not your actual string. I'm trying to establish what you regard as a word/name/

  6. #6
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Find the position of a substring in a string

    Try this
    Function CountWords(rng As Range, str2find As String)
      Dim posn As Long
      Dim strTemp As String
      
      posn = InStr(rng, str2find)
      strTemp = Left(rng, posn - 1)
      
      CountWords = Len(strTemp) - Len(Replace(strTemp, " ", "")) + 1
    End Function
    Use in Excel like so, where A2 is your string and B2 is the string to find, Drag Down as required.
    Formula: copy to clipboard
    =CountWords(A2,$B$1)


    Or
    Formula: copy to clipboard
    =CountWords(A2,"Jeremy Wade")


    Or with VBa
    WordNo = CountWords(ActiveSheet.Range(A2),"Jeremy Wade")
    Last edited by Marcol; 10-21-2012 at 02:09 PM.

  7. #7
    Registered User
    Join Date
    06-08-2012
    Location
    Bucharest, Romania
    MS-Off Ver
    Excel 2003
    Posts
    50

    Re: Find the position of a substring in a string

    Hello Marcol,
    Yes, your function is getting exactly what I want. A beautiful code line that will make my life easier.
    For those who want this function in a vb standard I have re-written it below.
    Function CountWords(bigString As String, subString As String)
          Dim posn As Long, strTemp As String, merge As String
        
          posn = InStr(1, bigString, subString)
          strTemp = Left$(bigString, posn - 1)
          merge = Replace(strTemp, " ", vbNullString)
          CountWords = Len(strTemp) - Len(merge) + 1
    End Function
    Thank you very much Marcol and thank you Richard too for your interest in my issue.

    Regards,
    Daniel

+ 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