Extract multiple specified values from cell and return in 1 cell

Dave87

Board Regular
Joined
Apr 22, 2020
Messages
107
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hello everyone,

I need some help on this, in column A i have a list of various fruit, within column C i have sentences which may contain one or more of the fruit listed in Column A.

In column E i would like to list the fruit from column A which is contained in column C.

Also i would like to remove any references to duplicate fruit - see cell E2 which only lists apple once

I'm currently using the following formula but it only returns the first 'fruit' listed in column A and not all the 'fruits' in column C
VBA Code:
{=IFERROR(INDEX($A$2:$A$6,MATCH(TRUE,ISNUMBER(SEARCH($A$2:$A$6,C2)),0)),"")}

Column A
Lookup Items
orange
apple
strawberry
pear
Kiwi

Column C
Within Text
Today I ate 2 apple and 3 orange. I like apple
tomorrow I will juice 1 pear​
this could be blank.​
I need to buy some apple, orange, kiwi and plum

Column E - Required Answer
apple
orange
pear
0
apple
orange
kiwi
plum

I have Excel 2016 and 365/online/onedrive

(work have blocked XL2BB & Google drive so i cant share a document)
 

Attachments

  • Mr Excel Lookup.png
    Mr Excel Lookup.png
    18.7 KB · Views: 18

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
I would be surprised if you get a successful formula using standard worksheet functions. Even your existing formula can return incorrect results. Try it with the sample column A fruits you have given and these two values in column C:
I like pineapple
Nothing appears here


Secondly, I assume that "plum" is an error in both places you have given expected results since that fruit does not appear in column A.

You could test this user-defined function.

VBA Code:
Function ListFruit(rFruitItems As Range, sText As String) As String
  Dim d As Object, RX As Object, M As Object

  Set d = CreateObject("Scripting.Dictionary")
  d.CompareMode = 1
  Set RX = CreateObject("VBScript.RegExp")
  RX.Global = True
  RX.IgnoreCase = True
  RX.Pattern = "\b(" & Join(Application.Transpose(rFruitItems.Value), "|") & ")\b"
  For Each M In RX.Execute(sText)
    d(CStr(M)) = 1
  Next M
ListFruit = Join(d.keys(), vbLf)
End Function

Column E would need to be set to 'Wrap Text'

Dave87 2020-07-02 1.xlsm
ABCDE
1Lookup ItemsWithin Text
2orangeToday I ate 2 apple and 3 orange. I like appleapple orange
3appletomorrow I will juice 1 pearpear
4strawberry 
5pearthis could be blank. 
6KiwiI need to buy some apple, orange, kiwi and plumapple orange kiwi
List Fruit
Cell Formulas
RangeFormula
E2:E6E2=ListFruit(A$2:A$6,C2)
 
Upvote 0
Solution
I am trying the same thing without macros, and I am half way there...

{=SUMPRODUCT(IF(ISNUMBER(SEARCH($A$1:$A$5,C1,1)),1,0),ROW($A$1:$A$5))}

I am able to find out which fruits have matches, but I am unable to display the text,

1593681892875.png
 
Upvote 0
I am half way there...

{=SUMPRODUCT(IF(ISNUMBER(SEARCH($A$1:$A$5,C1,1)),1,0),ROW($A$1:$A$5))}

I am able to find out which fruits have matches, but I am unable to display the text,
I'm sorry, but I do not think you are even half way there. Consider this example with your formula. It is showing a match for both apple and pear, neither of which words actually occur in C4

1593684649557.png
 
Upvote 0
Yes, you are correct, it gives a false positive...
a very cheap solution that I found is to add space before the words in column A (this will count words like "apple,", "apples", "apple.")
But as you pointed out with the earlier solution, it isn't a robust solution (Sam as a hit for Samson)...
 
Upvote 0
a very cheap solution that I found is to add space before the words in column A
You would have to add a space after the words as well as before. And that still does not resolve the issue of how to actually extract & list the words
 
Upvote 0
Glad it worked for you. Thanks for the follow-up. :)

One thing that I forgot to mention: The list in column A should not contain blank cells. If that was possible, then I would modify the function to solve the resulting problem. Post back if that is required.
 
Upvote 0
One thing that I forgot to mention: The list in column A should not contain blank cells. If that was possible, then I would modify the function to solve the resulting problem. Post back if that is required.
No blank cells so its working perfect. thank you very much
 
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

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