VBA "contains" or "like" statement?

Captain Smith

Active Member
Joined
Feb 28, 2003
Messages
324
I have searched this board and I know somewhere there must be a post like this already, but I cannot find it.

I just want to write a simple VBA statement to identify if a cell contains a certain string, rather than the entire string being equal or not equal to a value.

Something like:
sub hello()
dim cell as range
For Each cell In Selection
if cell.contains (*pickle*) then msgbox ("I like pickles")
Next
End Sub

I know this is the wrong syntax but what is the right syntax?

Thanks!
 
Sorry,Didn't realise you wanted contains
try
Code:
Sub MM1()
Dim lc As Integer, c As Integer, r As Long
lc = Cells(11, Columns.Count).End(xlToLeft).Column
For c = lc To 1 Step -1
    If Not Cells(11, c) Like "objtype" And Not Cells(11, c) Like "assessment" Then
        Columns(c).EntireColumn.Hidden = True
    End If
Next c
For r = 12 To 150
    If Cells(r, 1) <> 4 Or Cells(r, 2) <> 4 And Cells(r, 2) <> 1 Or Cells(r, 5) <> 4 Or Cells(r, 11) <> 4 Or Cells(r, 15) <> 4 Then
        Rows(r).EntireRow.Hidden = True
    End If
Next r
End Sub
 
Upvote 0

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Michael, I think you're on the wrong thread...

Coffee time mate :).
 
Upvote 0
:rofl::rofl:....what an idiot...I had 2 versions of Mr Excel open and answered the wrong one.......:banghead:
I wondered where the answer to that thread went...:huh:
 
Upvote 0
I am looking for something similar, but instead of hiding a row, I want to place specific text in the cell adjacent (with multiple else statements). So for example, if A1 is "pickles", B1 should say "Topping", if A2 says "Ketchup", B2 should say "Condiment" and so on...
 
Upvote 0
Welcome to the forum.

It sounds more like you want a lookup formula of some type, rather than code.
 
Upvote 0

Forum statistics

Threads
1,223,319
Messages
6,171,427
Members
452,402
Latest member
siduslevis

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