Checking if a cell contains a certain word

Ruky

New Member
Joined
Sep 20, 2011
Messages
9
Hello,
I have cells A1 to A50 and there are different types and lengths of text in each cell. But some cells have a word in common (say the word is "apple").

I want to loop through each cell to check for the word apple and if there's a match another set of commands are activated in the VBA. I am just not aware of any function in VBA that will allow me to check for the word I want to find the range of cells.

Does anyone know if such a function exist? If yes, what's the syntax for this.

Thanks,
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
The VBA function is Instr...
Code:
Sub a()
Dim r As Range

For Each r In Range("A1:A50")
    If InStr(1, r, "apple", 1) > 0 Then
        'your code here
    End If
Next r
End Sub
 
Upvote 0
Assuming you won't be searching for "apple" every time, perhaps this slight tweak to njimack's suggestion...
Code:
Sub aa()
Dim SearchTerm As String, r As Range
SearchTerm = InputBox("Enter the string to search for...", "Search Term")
For Each r In Range("A1:A50")
    If InStr(r, SearchTerm) Then
        'your code here
        MsgBox "Found " & SearchTerm & " in " & r.Value
    End If
Next r
End Sub
Hope it helps.

EDIT:
If you want to make this so the inputbox entry is not case sensitive, add this line to the very top of your code module:
Option Compare Text
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,551
Messages
6,179,476
Members
452,915
Latest member
hannnahheileen

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