VBA: If cell contains word (from a list) then return a specific value

Kikisinche

New Member
Joined
Nov 16, 2017
Messages
4
The following scenario was already addressed on another post:
In the following table I would like the code to return “Fruits” if the cell contains a word that refers to a fruit (i.e. apples, oranges, pears)
I love applesFruits
I like orangesFruits
I bla bla lettuceVeggies
I hate milkDrinks
I blabla pearsFruits
I blabla books“NOT LISTED”

<tbody>
</tbody>


I would like to add some more categories, for example:
for example "Vegetables" that would include Lettuce, Tomato then print "Veggies”, another category could be caller “Drinks” and would include milk, coffee..
In addition, let’s say that in case the content of the cell tested is not matching with any of these categories, it would return “NOT LISTED”


I know that in the initial example, only the fruits were included and the code was the one listed below, I am just not too sure on how to integrate the 2nd, 3rd categories as well as the message in case nothing is matching.

Sub Fruit()
Dim LR As Long, i As Long, j As Long, Fruits
Fruits = Array("apple", "orange", "pear")
LR = Range("A" & Rows.Count).End(xlUp).Row
For i = 1 To LR
With Range("A" & i)
For j = LBound(Fruits) To UBound(Fruits)
If .Value Like "*" & Fruits(j) & "*" Then
.Offset(, 1).Value = Fruits
Exit For
End If
Next j
End With
Next i
End Sub




I would love to get clarification on this.
Thanks so much for your time/help,
Kev
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
for example "Vegetables" that would include Lettuce, Tomato

Tomato is a fruit, not a vegetable.

Perhaps you could have a two column list on a separate sheet (hidden if desired) that has all categories and items.
Then loop the list for each cell in the range to find the item and return the category.
 
Upvote 0

Forum statistics

Threads
1,214,808
Messages
6,121,686
Members
449,048
Latest member
81jamesacct

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