newuser
newuser

Reputation: 69

VB Macro Like Operator

I am very new to VB macro.In sheet 3 i have three columns named "Country " "Provinces" and "Risk" For "Provinces", values present are NB,NS,NF,PE. IN sheet5, i have written a code like

`(val) Like "*[HH,HHJ,qqw,www]" Then

to check if user has provided input like NB or like NB, NS or like PE only then corresponding values from Risk will get displayed. Now with the above code , if user is entering value as "," also results is getting displayed as , is present in the Like statement.

Kindly guide me.

Upvotes: 0

Views: 202

Answers (1)

Ron Rosenfeld
Ron Rosenfeld

Reputation: 60344

You can't use VBA Like that way. Try:

X = "abc, NB"
If X Like "*NB" Or _
    X Like "*NS" Or _
    X Like "*NF" Or _
    X Like "*PE" Then

    Debug.Print "X is in the Maritimes"
End If

If you set Option Compare Text at the beginning of your macro, you don't have to Ucase the string being tested.

For complex comparisons, you can use Regular Expressions in VBA. You need to set the appropriate reference.

Upvotes: 2

Related Questions