Select Case Like

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,924
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Why does this work:

Code:
If Sheet1.Cells(1,1).Value Like "a*" Then MsgBox "Starts with a"

but this fails?

Code:
Select Case Sheet1.Cells(1,1).Value

    Case Like "a*"

        Msgbox "Starts with a"

   Case Else

        Msgbox "Doesn't"

End Select

EDIT SORTED:

Code:
Select Case Sheet1.Cells(1,1).Value Like "a*"

    Case True

        Msgbox "Starts with a"

   Case Else

        Msgbox "Doesn't"

End Select

Thanks
 
Last edited:

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Why would you use that convoluted construction, when a simple If...Then is so much easier, and probably more efficient?
 
Upvote 0
You can also reverse the logic:

Code:
Select Case True
    Case Sheet1.Cells(1, 1).Value Like "a*"
        MsgBox "Starts with a"
    Case Sheet1.Cells(1, 1).Value Like "b*"
        MsgBox "Starts with b"
    Case Else
        MsgBox "Doesn't start with a or b"
End Select

Which is sometimes useful.

WBD
 
Upvote 0
Why would you use that convoluted construction, when a simple If...Then is so much easier, and probably more efficient?


Good question. No idea, I didn't write it!




You can also reverse the logic:

Code:
 Select Case True
     Case Sheet1.Cells(1, 1).Value Like "a*"
         MsgBox "Starts with a"
     Case Sheet1.Cells(1, 1).Value Like "b*"
         MsgBox "Starts with b"
     Case Else
         MsgBox "Doesn't start with a or b"
 End Select

Which is sometimes useful.

WBD

Thanks
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,901
Messages
6,175,277
Members
452,629
Latest member
SahilPolekar

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