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
 
Mostly "/", "-" and "."

They are mostly the part of the word.
You cannot tell vba to "mostly" do something. Those three punctuation symbols all have a special meaning in regular expression syntax, which is what I am using. They would need special treatment to deal with them but to determine the best way to do that I would need to see some good representative sample data and know all non-alphabetical and non-numerical characters that can occur as some may require different treatment.

I would also like to know whether any such punctuation does or can occur in the column A values and/or the column C values in the layout that we have been using.

Looks like numerical data is now also to be considered - not mentioned before. If "7.00MM" occurred in both column A and C2 do you consider that a "word" when counting how many words are found?

Any chance that we could still have this?
could you post a few sets of representative sample data that shows the sort of punctuation variation that can occur?
 
Upvote 0

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Stick to your 1st sample
An approach with UDF (User define formula)
Right click on tab name, View code, Insert, Module then paste below code into:
VBA Code:
Option Explicit
Function BestMatch(rng As Range, cell As Range)
Dim i&, pos&, max&, count&, ce As Range, sA, st As String
For Each ce In rng
    sA = Split(ce): count = 0: pos = 0
    For i = 0 To UBound(sA)
        pos = InStr(pos + 1, ce, sA(i))
        If pos > 0 Then count = count + 1
    Next
    If count > max And count = UBound(sA) + 1 Then
        max = count
        'BestMatch = ce.Value ===> if expect text string
        'BestMatch = ce.Address(0, 0) ===> if expect cell address
        BestMatch = ce.Row '===> if expect row index
    End If
Next
End Function
I leave 3 options of outcome: Textstring, cell address or cell row
BestMatch(rng As Range, cell As Range)
Assums part string in A1:A4 (rng), original string in C2 (cell)
In D2, type:
Code:
=BestMatch($A$1:$A$4,$C$2)

Capture.JPG
 
Upvote 0
Hi @Flashbond , If you end up going with a UDF, you could check the UDF from post #2 again, I just made a few small changes with the new information you provided:

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

Dante Amor
ABCD
1Electrical1Result
2Insulated Gloves2Electrical Insulated Gloves2
3Electrical Gloves3
4
5The quick brown over lazy dog1Result
6fox2The quick brown fox jumps over the lazy dog.1
7jumps over the lazy3
8dog4
9
10jumps over the lazy1Result
11fox2The quick brown fox jumps over the lazy dog.3
12The quick brown over lazy dog3
13dog4
Hoja1
Cell Formulas
RangeFormula
D2D2=contains_words(A1:A3,C2)
D6,D11D6=contains_words(A5:A8,C6)



If there is any case that the UDF does not comply with, then I join Peter's request:

Any chance that we could still have this?

could you post a few sets of representative sample data that shows the sort of punctuation variation that can occur?
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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