Lookup for cell contains most of the words in a sentence.

  • Thread starter Thread starter Legacy 143009
  • Start date Start date
L

Legacy 143009

Guest
Hello everyone,

I am looking for a formula input to D2. The aim is to find the cell in Column A that contains most of the words given in C2 and return Column B value.

Column AColumn BColumn CColumn D
1The quick brown1Given sentence:Desired result:
2fox2The quick brown fox jumps over the lazy dog.3
3jumps over the lazy3
4dog4
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Try with this UDF:

VBA Code:
Function contains_words(txt As String, rng As Range)
  Dim c As Range
  Dim nmax As Long, n As Long
  
  For Each c In rng.Columns(1).Cells
    If InStr(1, txt, c.Value) > 0 Then
      n = UBound(Split(c.Value, " ")) + 1
      If n > nmax Then
        nmax = n
        contains_words = c.Offset(, 1).Value
      End If
    End If
  Next
End Function


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 contains_words just like it was a built-in Excel function. For example:

Dante Amor
ABCD
1The quick brown1Given sentence:Desired result:
2fox2The quick brown fox jumps over the lazy dog.3
3jumps over the lazy3
4dog4
Hoja1
Cell Formulas
RangeFormula
D2D2=contains_words(C2,A2:B4)


---
 
Upvote 0
Thanks for your effort. I am looking for a pure formula solution if possible.
 
Upvote 0
In column B I put other numbers to do my tests.
Try:

Dante Amor
ABCD
1The quick brown12Given sentence:Desired result:
2fox13The quick brown fox jumps over the lazy dog.14
3jumps over the lazy14
4dog15
5some words to compare16
Hoja1
Cell Formulas
RangeFormula
D2D2=INDEX(B1:B5,MAX(IF(LEN(A1:A5)-LEN(SUBSTITUTE(A1:A5," ",""))+1= LARGE(IF(ISNUMBER(SEARCH(A1:A5,C2)),LEN(A1:A5)-LEN(SUBSTITUTE(A1:A5," ",""))+1),1), IF(IF(ISNUMBER(SEARCH(A1:A5,C2)),LEN(A1:A5)-LEN(SUBSTITUTE(A1:A5," ",""))+1),ROW(B1:B5)))))
Press CTRL+SHIFT+ENTER to enter array formulas.
 
Upvote 0
In column B I put other numbers to do my tests.
Try:

Dante Amor
ABCD
1The quick brown12Given sentence:Desired result:
2fox13The quick brown fox jumps over the lazy dog.14
3jumps over the lazy14
4dog15
5some words to compare16
Hoja1
Cell Formulas
RangeFormula
D2D2=INDEX(B1:B5,MAX(IF(LEN(A1:A5)-LEN(SUBSTITUTE(A1:A5," ",""))+1= LARGE(IF(ISNUMBER(SEARCH(A1:A5,C2)),LEN(A1:A5)-LEN(SUBSTITUTE(A1:A5," ",""))+1),1), IF(IF(ISNUMBER(SEARCH(A1:A5,C2)),LEN(A1:A5)-LEN(SUBSTITUTE(A1:A5," ",""))+1),ROW(B1:B5)))))
Press CTRL+SHIFT+ENTER to enter array formulas.
Flowless
 
Upvote 0
Does that mean your data could not be like this where all 6 of the words in cell A1 are in C2?

Flashbond.xlsm
ABCD
1The quick brown over lazy dog1Given sentence:Desired result:
2fox2The quick brown fox jumps over the lazy dog.3
3jumps over the lazy3
4dog4
Sheet1
Cell Formulas
RangeFormula
D2D2=INDEX(B1:B5,MAX(IF(LEN(A1:A5)-LEN(SUBSTITUTE(A1:A5," ",""))+1=LARGE(IF(ISNUMBER(SEARCH(A1:A5,C2)),LEN(A1:A5)-LEN(SUBSTITUTE(A1:A5," ",""))+1),1),IF(IF(ISNUMBER(SEARCH(A1:A5,C2)),LEN(A1:A5)-LEN(SUBSTITUTE(A1:A5," ",""))+1),ROW(B1:B5)))))
Press CTRL+SHIFT+ENTER to enter array formulas.
 
Upvote 0
Does that mean your data could not be like this where all 6 of the words in cell A1 are in C2?

Flashbond.xlsm
ABCD
1The quick brown over lazy dog1Given sentence:Desired result:
2fox2The quick brown fox jumps over the lazy dog.3
3jumps over the lazy3
4dog4
Sheet1
Cell Formulas
RangeFormula
D2D2=INDEX(B1:B5,MAX(IF(LEN(A1:A5)-LEN(SUBSTITUTE(A1:A5," ",""))+1=LARGE(IF(ISNUMBER(SEARCH(A1:A5,C2)),LEN(A1:A5)-LEN(SUBSTITUTE(A1:A5," ",""))+1),1),IF(IF(ISNUMBER(SEARCH(A1:A5,C2)),LEN(A1:A5)-LEN(SUBSTITUTE(A1:A5," ",""))+1),ROW(B1:B5)))))
Press CTRL+SHIFT+ENTER to enter array formulas.
Oh, I've never thought of that scenario. Yes, it should be any of the words. In your scenario, the desired result should be 1. Tnaks a lot @Peter_SSs ! Actually, I haven't checked @DanteAmor 's suggestion against this scenario.
 
Upvote 0
Then in that case I suspect you will need a macro.
There is a further question though. In the examples so far all of the words in each cell in column A exist in the C2 value. Is that always the case or could you have an example like this where the words "red and "cat" in column A do not exist in C2?

Flashbond.xlsm
ABC
1The quick red1Given sentence:
2cat2The quick brown fox jumps over the lazy dog.
3jumps over the lazy3
4dog4
Sheet1
 
Upvote 0
Oh yes. You always look through the pinhole @Peter_SSs ! A more precise example would be:

Column AColumn BColumn CColumn D
1the lazy fox1 (3 matches)Given sentence:Desired result:
2the quick dog jumps2 (4 matches)The quick brown fox jumps over the lazy dog2
3over the brown cat3 (3 matches)
4jumps cat over4 (2 matches)
I think the example above is only achievable via VBA.

If the words are in order, is still VBA required? Like the example below:
Column AColumn BColumn CColumn D
1jumps over the cat1 (3 matches)Given sentence:Desired result:
2the lazy dog2 (3 matches)The quick brown fox jumps over the lazy dog3
3the brown cat jumps over3 (4 matches)
4fox over cat4 (2 matches)
 
Upvote 0
I'm not sure that I will have an answer for you either way, but I can see more possible confusion - or at least different ways to interpret things.
For the sample below
  1. Does A1 have two matches or one for "quick"?
  2. Does A2 have two matches or one for "the" (since "the" occurs twice in C2)?
  3. Does A3 have two matches or one for "the" (there are certainly two of each in each cell but perhaps it is only considered as one word)?
Flashbond.xlsm
AC
1A quick cat and a quick ratGiven sentence:
2The slow horseThe quick brown fox jumps over the lazy dog.
3The cat and the rat
Sheet1
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,213
Members
452,618
Latest member
Tam84

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top