search within a column for specific words and display the found words in the adjacent cells

sammy1981

New Member
Joined
Jun 28, 2018
Messages
20
[FONT=Helvetica Neue, Helvetica, Arial, sans-serif]see attached file, im trying to find out if any of the ingredients from column D exists in column A and display whatever ingredient is found in column B, is this possible? need to go through lots of ingredients and find anything that needs special instructions [/FONT]
[FONT=Helvetica Neue, Helvetica, Arial, sans-serif]see below example[/FONT]
butylhydroxyanisole, lavender oil, methylparaben, purified landolin, purfied water A-alpha-C (2-Amino-9H-pyrido[2,3-b]indole)
Corn Starch, d&c Red #27 Aluminum Lake, d & c Red #30 Aluminum Lake, Flavors, Saccharin Sodium A-alpha-C (2-Amino-9H-pyrido[2,3-b]indoleAbiraterone acetate
CarboxyMethylcellulose sodium, Microcrystalline Cellulose,flavor, Acetylaminofluorene, Purified Water, Red 22, Red 28, Salicylic Acid, Acetaldehyde
FD&C red #40 , propylene glycol, flavoring, sucrose, and water, soybean, oil and corn starch used as processing aids.Acetamide
Famotidine, USP 20mg...... Acid ReducerAcetazolamide
Purified Water, Citric Acid, Sodium Benzoate Octoxynol-9.Acetochlor
Adipic Acid, FD&C Blue 1, FD&C Red 27 , FD&C Yellow 6, Acetaldehyde FD&C Yellow 10Acetohydroxamic acid
Polyehtylene, Sodium Sarcoisnate, EDTA, Quaternium-15, Carbomer, acetate2-Acetylaminofluorene

<tbody>
</tbody>
[FONT=&quot]
[/FONT]
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
in the file i attached i dont know necessarily that any ingredients in column D would be found in column A but i need to go through lists and lists of ingredients and check if any do have them
for example i would want to find the ingredient Oxybenzone in this sunscreen and all the ingredients are in the same cell "Homosalate 10.0%; Octisalate 5.0%; Oxybenzone 5.0%; Avobenzone 2.0%; Octocrylene 2%, SD Alcohol 40; Acrylates/Octylacrylamide Copolymer; Glycerin; Fragrance"
 
Upvote 0
If you do not know the expected outcome, it makes it very difficult to write a macro that works.
 
Upvote 0
This
Code:
Sub ListIngredients()
   Dim Cl As Range
   Dim Dic As Object
   Dim sp As Variant
   Dim i As Long
   
   Range("A:A").Replace "/", ",", xlPart, , , , False, False
   Set Dic = CreateObject("scripting.dictionary")
   For Each Cl In Range("D2", Range("D" & Rows.Count).End(xlUp))
      Dic(Cl.Value) = Empty
   Next Cl
   For Each Cl In Range("A2", Range("A" & Rows.Count).End(xlUp))
      sp = Split(Cl.Value, ",")
      For i = 0 To UBound(sp)
         If Dic.exists(Trim(sp(i))) Then Cl.Offset(, 1) = Trim(sp(i)) & ", " & Cl.Offset(, 1).Value
      Next i
   Next Cl
End Sub
gives only one result. Is that correct?

whats this macro suppose to do?
 
Upvote 0
If you do not know the expected outcome, it makes it very difficult to write a macro that works.

i want that whatever ingredient is found to display it in column B and whatever is not found leave the cell in column B empty or just put NA
 
Upvote 0
@Fluff: Terrific solution!
Would you mind explaining:
Code:
Dic(Cl.Value) = Empty

Can I assume that this line:
Code:
If Dic.exists(Trim(sp(i))) Then Cl.Offset(, 1) = Trim(sp(i)) & ", " & Cl.Offset(, 1).Value
takes into account the possibility that a cell in column A may contain 2 or more different ingredients?
 
Upvote 0
@Fluff: Terrific solution!
Would you mind explaining:
Code:
Dic(Cl.Value) = Empty
It adds the value of CL to the dictionary (if it doesn't already exist) & makes the item "empty".
It basically does the same as
Code:
If Not Dic.exists(Cl.Value) Then Dic.Add Cl.Value, Nothing

Can I assume that this line:
Code:
If Dic.exists(Trim(sp(i))) Then Cl.Offset(, 1) = Trim(sp(i)) & ", " & Cl.Offset(, 1).Value
takes into account the possibility that a cell in column A may contain 2 or more different ingredients?
Thats right :)
 
Upvote 0

Forum statistics

Threads
1,215,390
Messages
6,124,667
Members
449,178
Latest member
Emilou

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