+ Reply to Thread
Results 1 to 7 of 7

Find the position of a substring in a string

  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 ?
    Please Login or Register  to view this content.
    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.
    Please Login or Register  to view this content.
    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 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
    Please Login or Register  to view this content.
    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
    Please Login or Register  to view this content.


    Or
    Formula: copy to clipboard
    Please Login or Register  to view this content.


    Or with VBa
    Please Login or Register  to view this content.
    Last edited by Marcol; 10-21-2012 at 02:09 PM.

  6. #6
    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/

  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.
    Please Login or Register  to view this content.
    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