For example, I have a long list of sentences (I like ice cream, I like apples, I like dogs, etc) and I need to search each of those sentences to find if each sentence has "cheese, bananas, or cats" and only those three things - that is all I care about. If the sentences dont say those three words, I need the result or return to be other BUT if the sentence does contain any one of those three words I need the result to return THAT word. - If the sentence is "I like cheese" the formula should return "cheese" and so on. I have tried many different formulas that either do not work OR it says i have too many conditions. Please help
-
Maybe related: MS Excel – assigning “categories” based on keywords. – Scott - Слава Україні May 09 '19 at 07:10
-
What have you tried so far? – dmb May 09 '19 at 13:27
-
1Possible duplicate of Extract Text String within Cell, compare with list and return value – fixer1234 May 13 '19 at 01:41
3 Answers
See how close this comes to what you're looking for.
Click on image for larger view
I may be assuming potential complexity that you don't have, so this might not be the simplest solution.
Column A contains the sentences. I assumed your examples just happened to have the target word at the end in every case, but that the word could appear anywhere in the sentence. If it will only ever appear at the end, there are simpler ways to look for it. I also assumed that there could be differences in capitalization, like if the word appears at the beginning of the sentence.
Column D contains a list of your target words. Putting them in a list simplifies things because most solutions will need to use the word for some kind of lookup, then use it again for the result. The list also simplifies changing the targets without needing to alter all of your formulas. If you really don't want the list, and even hiding it isn't enough, you can hard code the values in the formula instead of using cell references.
Column B contains the result. I assumed you only care about the target word and not its capitalization (you want the same result regardless of where it appears in the sentence or its capitalization). This formula leaves the cell blank if there is no match (actually containing a null). This is more efficient for finding the matching records than sticking some kind of result in every cell. If you want something else to show for those cases, the formula will be more complicated.
The formula in B1 (copy it down the column as needed, or pre-populate a bigger range than you need), is:
=IF(ISNUMBER(SEARCH($D$1,A1)),$D$1,"")&IF(ISNUMBER(SEARCH($D$2,A1)),$D$2,"")&IF(ISNUMBER(SEARCH($D$3,A1)),$D$3,"")
This just searches (non-case-sensitive) for each target word. If it finds the word, it adds it to the result string. Otherwise, it adds a null. The ampersands concatenate the results for each target. So the result string will contain either null or the matching word.
Since the question states that you only have a few target words, this isn't too unwieldy. If you could actually have a much longer list of target words, it could make sense to use a different approach.
- 27,486
If you write text from A1 on column A
=RIGHT(SUBSTITUTE(A1;" ";"~";LEN(A1)-LEN(SUBSTITUTE(A1;" ";"")));LEN(A1)-FIND("~";SUBSTITUTE(A1;" ";"~";LEN(A1)-LEN(SUBSTITUTE(A1;" ";"")))))
is your solution
- 1
- 1
-
(1) This is an answer to a different question. (2) When you post an answer like this to a question like this, you should explain how it works and what it does. Showing sample data is a good idea. – Scott - Слава Україні May 09 '19 at 06:53
-
This just finds the last word in the sentence. The question is looking for only specific words, and requires a different response if it isn't one of those. Also, although the targets happened to be the last work in each example, the implication is that the target word could be anywhere in the sentence. – fixer1234 May 09 '19 at 07:01
-
OK, thanks for posting sample data for your answer. Now, please, read the question carefully (and read @fixer1234’s explanation of what you got wrong), and try to write an answer for *this* question. And explain it. – Scott - Слава Україні May 09 '19 at 07:06
-
P.S. @fixer1234: Congratulations on getting four zeros in a row. :-) – Scott - Слава Україні May 09 '19 at 07:06
-
-
@fixer1234: You hit 20000 a couple of days ago, didn’t you? – Scott - Слава Україні May 09 '19 at 07:29
-
=IF(OR(RIGHT(SUBSTITUTE(A1;" ";"~";LEN(A1)-LEN(SUBSTITUTE(A1;" ";"")));LEN(A1)-FIND("~";SUBSTITUTE(A1;" ";"~";LEN(A1)-LEN(SUBSTITUTE(A1;" ";""))))) = "Cats";RIGHT(SUBSTITUTE(A1;" ";"~";LEN(A1)-LEN(SUBSTITUTE(A1;" ";"")));LEN(A1)-FIND("~";SUBSTITUTE(A1;" ";"~";LEN(A1)-LEN(SUBSTITUTE(A1;" ";""))))) = "Bananas";RIGHT(SUBSTITUTE(A1;" ";"~";LEN(A1)-LEN(SUBSTITUTE(A1;" ";"")));LEN(A1)-FIND("~";SUBSTITUTE(A1;" ";"~";LEN(A1)-LEN(SUBSTITUTE(A1;" ";""))))) = "Cheese");RIGHT(SUBSTITUTE(A1;" ";"~";LEN(A1)-LEN(SUBSTITUTE(A1;" ";"")));LEN(A1)-FIND("~";SUBSTITUTE(A1;" ";"~";LEN(A1)-LEN(SUBSTITUTE(A1;" ";""))))); "")
This look crazy but it works!I just modified the first answer above.
-
1It will be easier for people to judge the answer if you show the results. But just guessing, it looks like this only deals with the target values if they are the last word in the sentence. The examples in the question were like that, but the description sounds like the target words can be anywhere in the sentence. – fixer1234 May 09 '19 at 14:00