+ Reply to Thread
Results 1 to 5 of 5

SELECT one, two, three, n words before and after a keyword in a string

  1. #1
    Registered User
    Join Date
    01-21-2022
    Location
    Australia
    MS-Off Ver
    Microsoft Office
    Posts
    3

    SELECT one, two, three, n words before and after a keyword in a string

    I have data in an excel sheet as below:

    id Text
    ------------
    1 There is a need to go for a PCR covid test if you have symptoms
    2 No need to go for a covid test if you don't have symptoms
    3 government increased test centres to conduct covid test to decrease the waiting time.

    Question: I need to search for 'covid test'(keyword: 'covid test') in each row and fetch the data as below:



    Result
    ----------------
    PCR covid test if you have
    a covid test if you don't
    conduct covid test to decrease the


    Logic: Need to search for the string 'covid test' in each row and display one word before 'covid test' and three words after 'covid test'.

    I'm not sure on making up the formula. Can any one please suggest?

  2. #2
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,203

    Re: SELECT one, two, three, n words before and after a keyword in a string

    For a good start, such a macro.
    Assumptions:
    1. It only finds one phrase (keyword) in each line
    2. Does not provide that the sentence begins with the searched phrase
    3. Does not predict that there are less than 3 words in a sentence after the searched phrase.
    Please Login or Register  to view this content.
    Artik
    Attached Files Attached Files

  3. #3
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,514

    Re: SELECT one, two, three, n words before and after a keyword in a string

    For the version with Spill Functionality.

    =IFERROR(GetMidSpil(A2,"covid test",1,3,1),"")
    copy down
    Where;
    A2 has data
    "covid test" = key string
    1 = word(s) before key word
    3 = word(s) after key words
    1 = compre mathod, 0 for case sensitive, 1 for case insensitive
    Please Login or Register  to view this content.
    For all versions

    =IFERROR(GetMid($A2,"covid test",1,3,COLUMN(A1),1),"")
    Copy right + down
    Where
    COLUMN(A1) = index reference when multiple match found
    Please Login or Register  to view this content.
    Attached Files Attached Files

  4. #4
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,926

    Re: SELECT one, two, three, n words before and after a keyword in a string

    bharath99 please do NOT post duplicate threads - I have closed your other 2 threads on this topic.
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  5. #5
    Forum Expert
    Join Date
    02-10-2019
    Location
    Georgia, USA
    MS-Off Ver
    Office 365
    Posts
    2,822

    Re: SELECT one, two, three, n words before and after a keyword in a string

    Here's a formula solution, albeit a very complicated one. I'm sure someone here can simplify this a lot, but I piece-mealed it together and this is what I came up with.

    Note that this is a little flexible and that it will work regardless of the phrase you use (doesn't have to be "covid test"). In this example, the phrase (or key words) is in cell B1 and the sentences are in cells B2:B4.

    The long formula is this:

    =MID(B2,SEARCH(CHAR(10),SUBSTITUTE(LEFT(B2,SEARCH($B$1,B2)-2)," ",CHAR(10),LEN(LEFT(B2,SEARCH($B$1,B2)-2))-LEN(SUBSTITUTE(LEFT(B2,SEARCH($B$1,B2)-2)," ",""))))+1,SEARCH($B$1,B2)-(SEARCH(CHAR(10),SUBSTITUTE(LEFT(B2,SEARCH($B$1,B2)-2)," ",CHAR(10),LEN(LEFT(B2,SEARCH($B$1,B2)-2))-LEN(SUBSTITUTE(LEFT(B2,SEARCH($B$1,B2)-2)," ",""))))+1)+LEN($B$1)+SEARCH(CHAR(10),SUBSTITUTE(RIGHT(B2,LEN(B2)-SEARCH($B$1,B2)-LEN($B$1))," ",CHAR(10),3)))

    You say your version is "Microsoft Office" so I don't know if you mean MS365 or not, but if you do, using the LET formula helps to explain it:

    Please Login or Register  to view this content.

+ 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: 1
    Last Post: 01-22-2022, 12:50 AM
  2. SELECT one, two, three, n words before and after a keyword in a string
    By bharath99 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 01-22-2022, 12:50 AM
  3. SELECT one, two, three, n words before and after a keyword in a string
    By bharath99 in forum Hello..Introduce yourself
    Replies: 3
    Last Post: 01-21-2022, 03:41 PM
  4. [SOLVED] Sum Only Whole Words for Word Matrix Based on a Keyword List
    By sweetrevelation in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 04-17-2014, 11:24 AM
  5. Return words before and after a keyword in excel
    By boriherter in forum Excel General
    Replies: 2
    Last Post: 01-09-2013, 09:23 PM
  6. [SOLVED] Need to chck if a keyword is present in a text string and return keyword if yes
    By Jekaterina in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 12-16-2012, 05:55 PM
  7. Replies: 8
    Last Post: 09-27-2012, 10:46 PM

Tags for this Thread

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