IF Statement for checking if a cell contains a certain word

jerryr0125

New Member
Joined
May 14, 2017
Messages
6
Hi - I am looking for an example of an example of a macro that would do the following:

It would an IF statement...
* Check to see if a cell contains a certain word (say "water").
* The word can be anywhere within the cell (say cell A2)
* If the cell contains the word THEN set cell B2 to a value (say W2).

The word 'water' can be anywhere in the cell A2 (such as "water in the well" or "there is water in the cup")

Thoughts ?? thanks - jerry
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Try:
Code:
Sub jerryr0125()
    If InStr(Range("A2"), "water") Then
        Range("B2").Value = Range("W2").Value
    End If
End Sub
 
Upvote 0
This assumes you do not care about case (can be upper or lower case) and you want something to happen if the word is not found. If you do not want anything to happen when the word is not found you can remove the else lines of code

Code:
Sub findword()


If InStr(UCase(Range("A2")), "WATER") Then
    Range("B2") = "W2"
Else
 'What should happen if water is not found?
 MsgBox ("water not found")
End If
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,323
Members
452,635
Latest member
laura12345

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