简体   繁体   中英

Excel: find a word in a cell, then return the preceding word and the found word

This is a slightly different take on a previous question I asked. I tried to modify the great advice I received but couldn't quite get there.

I am looking to find text in a cell (not case specific), then return the preceding word plus the found text. I prefer a formulaic solution (over a macro).

Sample cell contents include the following where I'm looking for " TRUCK ":

"This is a test dump TRUCK like it 54" - I want " dump TRUCK "

"This is pick-up Truck forgot 346 I like" - I want " pick-up Truck "

I tried some combo of the Mid() function but wasn't quite getting it:

=MID(B2,(FIND("Truck",B2,1)-1),FIND(" ",B2,FIND("Truck",B2,1)-1)-FIND("Truck",B2,1)+1)

Thanks for the help!

=TRIM(RIGHT(SUBSTITUTE(LEFT(B2,SEARCH(" Truck ",B2)+5)," ",REPT(" ",99)),199))

LEFT(B2,SEARCH(" Truck ",B2)+5) gets the " This is a test dump TRUCK " part.

Then SUBSTITUTE( ^ ," ",REPT(" ",99)) gets:

This                                                                                                   is                                                                                                   a                                                                                                   test                                                                                                   dump                                                                                                   TRUCK

and =TRIM(RIGHT( ^ ,199)) gets the last 2 words from the above and removes the extra spaces.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM