search for multiple keywords in a cell and return positive resuly when ALL are present

dori2o

Board Regular
Joined
Apr 7, 2016
Messages
72
I'm creating a quiz sheet in excel where the user inputs their answer into a cell. (i.e Cell F5)

The idea is that if they enter their answer into the answer cell the cell in the next column of that row will show a 'Tick' (uppercase P in windings font) if the answer is corect. At all other times the cell shows a 'cross' (uppercase O in windings).

Once all questions have been answered and all are correct they can submit the worksheet to an email address for the sheets to be verified and a winner chosen at random.

In order to get the tick in the cell the answer must contain certain keywords that are essential to the answer. i.e. the answer cannot be correct without these keywords.

I'm using =IF(SUMPRODUCT(--ISNUMBER(SEARCH(Y18:Y20,F5))),"P","O") to check for keywords in cell F5.

However, currently the formula returns a positive result even if there is only 1 word from the keywords list (held in range Y18:Y20) within the answer.

How would I update the formula so that all of the words in the list have to present within the text string before a positive result is returned.

Also can anyone confirm that having blank cells in the keywords list will have no effect on the answer, and if so how would I rectify this?

Many Thanks in advance.
 

JoeMo

MrExcel MVP
Joined
May 26, 2009
Messages
16,666
Try:
Code:
=IF(SUMPRODUCT(--(Y18:Y20<>"")*ISNUMBER(SEARCH(Y18:Y20,F5)))=ROWS(Y18:Y20),"P","O")
EDITED to include case where list (Y18:Y20) contains blank cells.
 
Last edited:

JoeMo

MrExcel MVP
Joined
May 26, 2009
Messages
16,666
Ignore post #2 . Testing shows that formula fails when there are blank cells in the list. I have to go offline now, hopefully someone else will provide a blanks-tolerant solution.:)
 

JoeMo

MrExcel MVP
Joined
May 26, 2009
Messages
16,666
Lightly tested, but I think this handles blank cells in your keyword list.
Code:
=IF(SUMPRODUCT(--ISNUMBER(SEARCH(Y18:Y20,F5))*(Y18:Y20<>""))=COUNTA(Y18:Y20),"P","O")
 

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
35,507
Office Version
2010
Platform
Windows
Lightly tested, but I think this handles blank cells in your keyword list.
Code:
=IF(SUMPRODUCT(--ISNUMBER(SEARCH(Y18:Y20,F5))*(Y18:Y20<>""))=COUNTA(Y18:Y20),"P","O")
Works so long as one of the words in Y18:Y20 is not embedded within a longer word. For example...

Y18: one
Y19: two
Y20: three

F5: two plus three (I'm done).
 
Last edited:

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
35,507
Office Version
2010
Platform
Windows
Here is a UDF (user defined function) that will correctly count words from the list only when they stand-alone (that is, when they are not embedded within other words) no matter what punctuations marks or spaces are next to them...
Code:
[table="width: 500"]
[tr]
	[td]Function MatchAll(Answer As String, WordList As Range) As String
  Dim R As Long, Cnt As Long, WordsCnt As Long, Words As Variant
  Words = Evaluate(Replace("IF(@="""","""",UPPER(@))", "@", WordList.Address))
  For R = 1 To UBound(Words)
    If Len(Words(R, 1)) Then
      WordsCnt = WordsCnt + 1
      If " " & UCase(Answer) & " " Like "*[!A-Z0-9]" & Words(R, 1) & "[!A-Z0-9]*" Then Cnt = Cnt + 1
    End If
  Next
  If Cnt = WordsCnt Then MatchAll = "P" Else MatchAll = "O"
End Function[/td]
[/tr]
[/table]
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 MatchAll just like it was a built-in Excel function. For example,

=MatchAll(F5,Y18:Y20)

If you are using XL2007 or above, make sure you save your file as an "Excel Macro-Enabled Workbook (*.xlsm) and answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.
 

admiral100

Well-known Member
Joined
Jan 17, 2015
Messages
873
Hi,

A formula version...and it solves the problem posted by Rick Rothstein in #5

Ctrl+Shift+Enter NOT just Enter

=IF(AND(Y18:Y20<>"",EXACT(TRIM(MID($F$5,IF(SEARCH(Y18,$F$5)-1=0,1,SEARCH(Y18,$F$5)-1),LEN(Y18)+1)),Y18),EXACT(TRIM(MID($F$5,IF(SEARCH(Y19,$F$5)-1=0,1,SEARCH(Y19,$F$5)-1),LEN(Y19)+1)),Y19),EXACT(TRIM(MID($F$5,IF(SEARCH(Y20,$F$5)-1=0,1,SEARCH(Y20,$F$5)-1),LEN(Y20)+1)),Y20)),"P","O")
 
Last edited:

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
35,507
Office Version
2010
Platform
Windows
Hi,

A formula version...and it solves the problem posted by Rick Rothstein in #5

Ctrl+Shift+Enter NOT just Enter

=IF(AND(Y18:Y20<>"",EXACT(TRIM(MID($F$5,IF(SEARCH(Y18,$F$5)-1=0,1,SEARCH(Y18,$F$5)-1),LEN(Y18)+1)),Y18),EXACT(TRIM(MID($F$5,IF(SEARCH(Y19,$F$5)-1=0,1,SEARCH(Y19,$F$5)-1),LEN(Y19)+1)),Y19),EXACT(TRIM(MID($F$5,IF(SEARCH(Y20,$F$5)-1=0,1,SEARCH(Y20,$F$5)-1),LEN(Y20)+1)),Y20)),"P","O")
I am not sure there are always going to be 3 words to match for each question. What if there are 10 words that needed to match? Or 20?

Also, I am not so sure your code addresses the possible problem JoeMo raised, namely, a blank cell within the word list in Column Y.
 

admiral100

Well-known Member
Joined
Jan 17, 2015
Messages
873
If it is more than 3 words then you are absolutely right , the formula will be long and cumbersome...
 

Aladin Akyurek

MrExcel MVP
Joined
Feb 14, 2002
Messages
85,138
Here is one which expects a space between the answers in F5...

=IF(SUMPRODUCT(ISNUMBER(SEARCH(" "&$Y$8:$Y$20&" "," "&F5&" "))+0)=COUNTIFS($Y$8:$Y$20,"?*"),"P","O")

When F5 = one two done x and Y8:Y20 consists of just one, two, and three, this formula will return O.
 

Forum statistics

Threads
1,082,246
Messages
5,363,984
Members
400,772
Latest member
solbebe

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top