Link to home
Start Free TrialLog in
Avatar of benspan
benspanFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Select Case combined with like operator

Hi,

I can't find a way to do what i'm after.

I need a select case function that will allow for the like operator.

eg..

case like "*ABC"
     blahblah
case like "*123*"
    blahblah2

I need to match the patern of the case to wildcard entries - but all I can find is that the like operator with wildcards won't work within a select case.

Any ideas anyone?

Thanks,

Benjamin.
Avatar of mccainz2
mccainz2

why not use an if - elseif structure ...

if case like "*abc"
Argghhh, hit enter on my touchoad ..

why not use an if - elseif structure ...

if case like "*abc"
.....
else if case like "*whatever"
.. etc
Hi benspan,
I suggest you have a look at regular expressions.

Search for regex here on EE, or have a look at this question

http://oldlook.experts-exchange.com/questions/20528014/code-needed-for-serial-number.html

The last answer has a link to Microsoft with even more information.

Learning Regular Expressions is not simple, but once you get the gist of it, you will see that it can solve not just this particular problem you are having, but many more, to the point that it may become addictive!


Dabas
you can also make use of this if using if-else statement:

If InStr(Text1.Text, "ABC") <> 0 Then
   blahblah
ElseIf InStr(Text1.Text, "123") <> 0 Then
    blahblah
End if
Avatar of benspan

ASKER

I'm trying to avoid a massive if then listing, so was trying to use the select case calls.

The instr is pretty much the like statement with wildcard values by the looks.

What i'm really trying to find out is if it is possible to use the select case statement to do fancy comparisons...

select case something

     case something like "*ABC*"
           x
     case something like "*BCD*"
           y

etc etc

the following format works:
select case something
    case is = "A"
          x
    case is = "B"
          y
etc etc, but this will only match exact values - whereas i want to match values with wildcards.

looks as though the case statement just can't handle this?
benspan,
> looks as though the case statement just can't handle this?
Right.
The case statement cannot handle this.
Regex is the way to go!

Dabas
can try something like this

Private Sub Command1_Click()
Dim x As String

x = InputBox("Enter Name")
Select Case True
Case x Like "*Appari"
    MsgBox "has Appari"
Case x Like "*Dabas"
    MsgBox "has Dabas"

End Select
End Sub
ASKER CERTIFIED SOLUTION
Avatar of appari
appari
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of benspan

ASKER

Perfect - exactly what i am trying to do.

you're definitely a guru.

Thanks.