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]
[TABLE="width: 500"]
<tbody>[TR]
[TD]butylhydroxyanisole, lavender oil, methylparaben, purified landolin, purfied water[/TD]
[TD] [/TD]
[TD]A-alpha-C (2-Amino-9H-pyrido[2,3-b]indole)[/TD]
[/TR]
[TR]
[TD]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]indole[/TD]
[TD][/TD]
[TD]Abiraterone acetate[/TD]
[/TR]
[TR]
[TD]CarboxyMethylcellulose sodium, Microcrystalline Cellulose,flavor, Acetylaminofluorene, Purified Water, Red 22, Red 28, Salicylic Acid, [/TD]
[TD][/TD]
[TD]Acetaldehyde[/TD]
[/TR]
[TR]
[TD]FD&C red #40 , propylene glycol, flavoring, sucrose, and water, soybean, oil and corn starch used as processing aids.[/TD]
[TD][/TD]
[TD]Acetamide[/TD]
[/TR]
[TR]
[TD]Famotidine, USP 20mg...... Acid Reducer[/TD]
[TD][/TD]
[TD]Acetazolamide[/TD]
[/TR]
[TR]
[TD]Purified Water, Citric Acid, Sodium Benzoate Octoxynol-9.[/TD]
[TD][/TD]
[TD]Acetochlor[/TD]
[/TR]
[TR]
[TD]Adipic Acid, FD&C Blue 1, FD&C Red 27 , FD&C Yellow 6, Acetaldehyde FD&C Yellow 10[/TD]
[TD][/TD]
[TD]Acetohydroxamic acid[/TD]
[/TR]
[TR]
[TD]Polyehtylene, Sodium Sarcoisnate, EDTA, Quaternium-15, Carbomer, acetate[/TD]
[TD][/TD]
[TD]2-Acetylaminofluorene[/TD]
[/TR]
</tbody>[/TABLE]
[FONT=&quot]
[/FONT]
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
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,224,827
Messages
6,181,197
Members
453,022
Latest member
RobertV1609

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