SELECT case (multiple condition) in VBA

JohnSeito

Active Member
Joined
Nov 19, 2007
Messages
390
Hi all,

I am wondering, does select case in VBA, can it do case this or that like an if statement.

This way it has flexibility, because without it, it doesn't have much flexibility as an if statement.

I tried to do select this or that but it doesn't work.

example code:

this is the normal case statement:
Code:
pick = cell(i, "F")

SELECT CASE pick
    CASE  a
         do this
    CASE b
         do this
    CASE c 
         do this
    END CASE

case statement with multiple condition: this one does not work.

Code:
pick = cell(i, "F")

SELECT CASE pick
    CASE  a or 1
         do this
    CASE b or 2
         do this
    CASE c or 3
         do this
    END CASE
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Instead of "or" use a comma to separate multiple values:

Code:
pick = cell(i, "F")

SELECT CASE pick
    CASE  a, 1
         do this
    CASE b, 2
         do this
    CASE c, 3
         do this
    END CASE
 
Upvote 0
With case select statements non related values are separated by commas (,) or you can also use To with a Range of values or the Is keyword with an expression.

Code:
For i = 1 To 6
    
        Result = Worksheets("Sheet1").Cells(i, 6).Value
        Select Case Result
            Case "a", 1
                MsgBox Result & " was case a To 1"
            Case "b", 2
                MsgBox Result & " was case b To 2"
            Case 3 To 6
                MsgBox Result & " was case c To 3"
            Case Is > 6
                MsgBox Result & " is greater than 6"
            Case Else
                MsgBox Result & " was none of the cases"
        End Select
                
    Next i

On a side note: Is it just me or are these forums really buggy? It's almost impossible to type and my cursor is constantly flashing :(
 
Last edited:
Upvote 0
Thanks for the info to both of you. I'll let you know if I have any other questions. The forums is fine on my side. :-/
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,213
Members
453,024
Latest member
Wingit77

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