T. P.
T. P.

Reputation: 121

VBA IF statement with multiple conditions

I have got an IF statement that looks like that:

If i=1 or i=3 or i=7 or i=9 or i=10 or i>=15 and i<=20 then
   'Do sth.
End If

My question is: Can I make these conditions easier (can I summarize them)?

Thanks in advance!

Upvotes: 1

Views: 1679

Answers (2)

pheeper
pheeper

Reputation: 1527

You could use Select Case. It would look something like this...

Sub test()
    Select Case i
        Case 1, 3, 7, 9, 10, 15, 16, 17, 18, 19, 20
            'do something
    End Select
End Sub

Upvotes: 1

Scott Craner
Scott Craner

Reputation: 152450

You can use select case:

Select Case i
    Case 1,3,7,9,10, 15 to 20
        'Do sth.
End Select

Upvotes: 4

Related Questions