Like expression giving wrong result

AlexanderBB

Well-known Member
Joined
Jul 1, 2009
Messages
1,958
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
x= "some text [2.02 stereo CD] more text"
? x Like ("*" & Chr$(91) & "* mono *" & Chr$(93) & "*")
True
But should be false. I do want the square brackets and spaces included.
Problem seems to be the square brackets. Can they be escaped ? Or some other fix?
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
I think not enough info for a solution yet. Maybe you can use Instr()? Depends on what you're really looking for. Is it [ ] or is it 2.02 stereo cd, or [2.02 stereo cd] or something else?
 
Upvote 0
The example is [2.02 stereo cd]
I'm using Instr now while developing but it won't be correct if the key words also appear in some other text.
 
Upvote 0
I think your problem is the inclusion of several characters/strings in the comparison. Like will return a match if you include [ in the search and [ is in the string. If you want spaces and brackets to be considered, that means you need to identify the exact pattern you need, because Like is based on pattern matching.
This returns True because the pattern match includes a left bracket:
Rich (BB code):
x = "some text [2.02 stereo CD] more text"
If x Like ("*[mono]*") Then
    MsgBox "true"
End If

This returns False because the pattern match does not include a left bracket:
Rich (BB code):
x = "some text [2.02 stereo CD] more text"
If x Like ("*mono*") Then
    MsgBox "true"
End If
Perhaps if you posted examples of inputs and expected results it would help anyone to help you.
 
Upvote 0
The text string to test has space-mono-Space or space-stereo-space inside square brackets.
The output is True or False.
Msg1 had an example where True was returned and it should have been false.
Maybe a Ref Ex pattern would be better ?
 
Upvote 0
You need to escape by adding [] like
Rich (BB code):
x Like "*[[]* mono *[]]*"
 
Upvote 0
Solution

Forum statistics

Threads
1,221,709
Messages
6,161,431
Members
451,705
Latest member
Priti_190

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