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

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
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,221,526
Messages
6,160,341
Members
451,638
Latest member
MyFlower

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