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!
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
sub hello1()
dim cell as range
For Each cell In Selection
if instr(1,cell,"pickle",1) then msgbox ("I like pickles")
Next
End Sub

sub hello2()
dim cell as range
For Each cell In Selection
if cell like "*pickle* then msgbox ("I like pickles")
Next
End Sub
 
Upvote 0
Thanks. I used the first method and it works great.
How would I change the first method to act on that cell if it does NOT
contain the word pickles?

Thanks again.
 
Upvote 0
Thanks. I used the first method and it works great.
How would I change the first method to act on that cell if it does NOT
contain the word pickles?

Thanks again.

Depends...

Only negative

sub hello2()
dim cell as range
For Each cell In Selection
if not instr(1,cell,"pickle",1) then msgbox ("I don't like pickles")
Next
End Sub

both

sub hello1()
dim cell as range
For Each cell In Selection
if instr(1,cell,"pickle",1) then
msgbox ("I like pickles")
else
msgbox ("I don't like pickles")
end if
Next
End Sub
 
Upvote 0
I only care about the negative situation. The not does not seem to work for me.
Here is my complete code. It hides rows that do contain the n value, which
it shouldn't. Any ideas? Thanks

Sub filterthis()
Dim cell As Range
n = InputBox("Enter word or phrase you want to filter", "Enter")
For Each cell In Selection
If Not InStr(1, cell, n, 1) Then cell.EntireRow.Hidden = True
Next
End Sub
 
Upvote 0
then try
Code:
Sub filterthis()
Dim cell As Range
n = InputBox("Enter word or phrase you want to filter", "Enter")
For Each cell In Selection
    cell.EntireRow.Hidden = (InStr(1, cell, n, 1) = 0)
Next
End Sub
 
Upvote 0
Hi, I wonder if you can help

I have some code

Code:
If Range("Q" & VariableRow) Like VariableString Then

How could I adapt that to use the instr command?
 
Upvote 0
It would be:

Code:
If Instr(1, Range("Q" & VariableRow).Value, VariableString) > 0 Then

If you don't want a case sensitive comparison, then you can use:
Code:
If Instr(1, Range("Q" & VariableRow).Value, VariableString, vbTextCompare) > 0 Then

or convert both strings to the same case explicitly:
Code:
If Instr(1, UCase$(Range("Q" & VariableRow).Value), UCase$(VariableString)) > 0 Then
 
Upvote 0

Forum statistics

Threads
1,224,829
Messages
6,181,224
Members
453,025
Latest member
Hannah_Pham93

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