+ Reply to Thread
Results 1 to 15 of 15

VBA Locate Text String and Extract Nth Word

  1. #1
    Forum Contributor
    Join Date
    12-28-2015
    Location
    US
    MS-Off Ver
    365
    Posts
    133

    VBA Locate Text String and Extract Nth Word

    Hi!

    I am trying to extract values from paragraphs. For example, there could be 1,500 characters equaling 200+ words. I would like to be able to locate a specific string of text (ie. "monday it will be") and then be able to extract the next two values 50 degrees - "monday it will be 50 degrees". I would like to be able to do this on a variable basis, not all paragraphs will be the same. My initial thought was to build a table that I would have to list the text strings I am looking for and the subsequent words, at least once, but then I could run the macro thereafter.

    see attached.

    thanks!

    Workbook removed at OP's request
    Last edited by AliGW; 11-11-2019 at 01:54 AM.

  2. #2
    Valued Forum Contributor
    Join Date
    02-02-2016
    Location
    Indonesia
    MS-Off Ver
    Office 365
    Posts
    1,001

    Re: VBA Locate Text String and Extract Nth Word

    Questions:
    1. where are the text with the paragraphs?
    2. could there be multiple match found? say there are multiple "monday it will be" in a paragraph.

  3. #3
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,408

    Re: VBA Locate Text String and Extract Nth Word

    Does this UDF (user defined function) do what you want...
    Please Login or Register  to view this content.
    where From is the text you want to search, What is the text you want to find and HowMany is the number of words after the text you want to find to be returned. Note that if the What text cannot be found, the ExtractIt function returns the empty text string (""). Note that if there are more than one occurrence of the What text string, this function only finds the first occurrence of it.


    HOW TO INSTALL UDFs
    ------------------------------------
    If you are new to UDFs, they are easy to install and use. To install it, simply press ALT+F11 to go into the VB editor and, once there, click Insert/Module on its menu bar, then copy/paste the above code into the code window that just opened up. That's it.... you are done. You can now use ExtractItjust like it was a built-in Excel function. For your posted example, you would use this formula in cell D2...

    =ExtractIt(D148,A2,C2)

    If you are using XL2007 or above, make sure you save your file as an "Excel Macro-Enabled Workbook (*.xlsm) and answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.
    Last edited by Rick Rothstein; 11-10-2019 at 11:42 AM.

  4. #4
    Forum Contributor
    Join Date
    12-28-2015
    Location
    US
    MS-Off Ver
    365
    Posts
    133

    Re: VBA Locate Text String and Extract Nth Word

    Questions:
    1. where are the text with the paragraphs?
    ans: the text could be anywhere in a given paragraph
    2. could there be multiple match found? say there are multiple "monday it will be" in a paragraph.
    ans: yes, i guess multiple matches could be found but I would likely expand the text string to make uniquely identifiable as possible (if that makes sense).

  5. #5
    Forum Contributor
    Join Date
    12-28-2015
    Location
    US
    MS-Off Ver
    365
    Posts
    133

    Re: VBA Locate Text String and Extract Nth Word

    to the proposed macro. I was able to "install" it and it worked when pointed at a single cell. Could I adjust to encompass a range of cells or even better the entire sheet?

    OR first the vba would have to locate the string "monday it will be" and then use that cell for the ExtractIt?
    Last edited by Excel4444; 11-02-2019 at 02:48 PM.

  6. #6
    Forum Contributor
    Join Date
    12-28-2015
    Location
    US
    MS-Off Ver
    365
    Posts
    133

    Re: VBA Locate Text String and Extract Nth Word

    I have been able to (with forum assistance) solve for looking into a range, but ran into something else. the macro brings the cell from the sheet with the data but only the cell address and not sheet2!A12, because I was hoping to reference that cell in the formula to return the words. attached is a sample with code.

    Workbook removed at OP's request
    Last edited by AliGW; 11-11-2019 at 01:55 AM.

  7. #7
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: VBA Locate Text String and Extract Nth Word

    Hello Excel4444.

    I have changed the layout of the sample workbook a little. to make it easier for you.

    Regards.

    Workbook removed at OP's request
    Last edited by AliGW; 11-11-2019 at 01:56 AM.
    Please consider:

    Be polite. Thank those who have helped you. Then Click on the star icon in the lower left part of the contributor's post and add Reputation. Cleaning up when you're done. If you are satisfied with the help you have received, then Please do Mark your thread [SOLVED] .

  8. #8
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,408

    Re: VBA Locate Text String and Extract Nth Word

    Quote Originally Posted by Excel4444 View Post
    attached is a sample with code.
    I have a question about one of your examples. You have in cell A5 you have sub-1234 as the word to find and on your data sheet you have this sub-123456687 in cell C6. Your example did not show an Nth Word for it, but if you had shown a 1 there, what would you have wanted as a result... 56687 or a blank cell (actually "")? In other words, are you looking for whole word matches or partial word matches? Said another way, if you wanted to look for the word "other" and on the data sheet was the word brotherhood, should the word other within it be the found word? If the answer is no, then you will need someone who knows Regular Expressions to come along and answer your question as any pure VBA only code solution will be very slow for large amounts of text as the test to exclude embedded words can be time-consuming without using Regular Expressions.

  9. #9
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: VBA Locate Text String and Extract Nth Word

    Hello Excel4444.

    I forgot to send you an updated sample workbook in Post #7.

    It is now attached in that same Post #7.

    Regards

  10. #10
    Forum Contributor
    Join Date
    12-28-2015
    Location
    US
    MS-Off Ver
    365
    Posts
    133

    Re: VBA Locate Text String and Extract Nth Word

    No worries Winon, really appreciate it!

    To answer Rick and follow up with Winon. if this can be done via vba, great, if not, formula will work. I was hoping to leverage the UDF ExtractIt pointed at column C, which has the cell address to return the words I need. So I wouldn't need to worry about Other in brotherhood, I would extend the string to include enough words to make it uniquely identifiable. I would like to be able type "monday it will be cold and" into A2 and then type the number 1 into B2 and have C2 return "50", because it went into sheet2 found "monday it will be cold and" which is part of the sentence "monday it will be cold and 50 degress", and it returned the first word from the end of sentence it went to find.

    So I tried combing two approaching with the vba that gets the cell address and formula, so i can't get that to work, but if can all be done with VBA i'm open. thanks for the help.


    updated workbook attached.

    Workbook removed at OP's request.
    Last edited by AliGW; 11-11-2019 at 01:53 AM.

  11. #11
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,408

    Re: VBA Locate Text String and Extract Nth Word

    Two follow up questions...

    1) For the text in cell A5, if you had put a 1 in D, what would you have wanted returned in Column F?

    2) Is the word "and" what you actually wanted for a result in Column F for the text in cell A3 (given the text in cell B3)? I am asking because I think you would have wanted "and thank" as the result which, if so, would mean my ExtractIt code needs to be modified.

  12. #12
    Forum Contributor
    Join Date
    12-28-2015
    Location
    US
    MS-Off Ver
    365
    Posts
    133

    Re: VBA Locate Text String and Extract Nth Word

    For cell A5 with a 1 in D would return nothing because there are no words after what’s found. And yes for it would “and thank”. My bad!

    Thanks
    Last edited by Excel4444; 11-07-2019 at 09:29 AM.

  13. #13
    Forum Contributor
    Join Date
    12-28-2015
    Location
    US
    MS-Off Ver
    365
    Posts
    133

    Re: VBA Locate Text String and Extract Nth Word

    thoughts of the follow ups?

  14. #14
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,408

    Re: VBA Locate Text String and Extract Nth Word

    Quote Originally Posted by Excel4444 View Post
    thoughts of the follow ups?
    Sorry, I lost track of this thread. As to your question, if you are writing the address as shown in Column C, then just use the INDIRECT function on it to retrieve that cell on that worksheet's value. For example...

    =ExtractIt(INDIRECT(C2),A2,D2)

  15. #15
    Forum Contributor
    Join Date
    12-28-2015
    Location
    US
    MS-Off Ver
    365
    Posts
    133

    Re: VBA Locate Text String and Extract Nth Word

    so worries. worked like a charm.

    thanks!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 4
    Last Post: 10-25-2016, 04:45 PM
  2. macro to locate locate text string and copy/paste offset range
    By guystanley in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-16-2014, 08:46 PM
  3. Formula to extract word out of text string in cell
    By Thonkhan in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 05-08-2014, 02:00 AM
  4. Extract Word from String
    By minhestrone in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-21-2013, 11:06 PM
  5. Extract specific word from a text string
    By krjoshi in forum Excel General
    Replies: 10
    Last Post: 01-19-2012, 02:00 PM
  6. Locate text in Word doc.
    By Mondeo in forum Excel General
    Replies: 2
    Last Post: 02-06-2010, 08:08 AM
  7. [SOLVED] Locate and count the recurrences of a text string
    By Trish2 in forum Excel General
    Replies: 1
    Last Post: 03-08-2006, 11:10 AM

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