Testing equality to a bunch of values for conditional statement

L

Legacy 96851

Guest
Just a general question. If I have something like:
Code:
If .Cells(1, ii) = "Comments" Or .Cells(1, ii) = "ABC" Or .Cells(1, ii) = "CDE" _
                                Or .Cells(1, ii) = "LEE" Or .Cells(1, ii) = "LSE" Then
is there a way to shorten it? I sort of assumed there was, but can't figure it out. Thanks!
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Sure, try this..

MyVals = Array("Comments","ABC","CDE","LEE","LSE")
x = Apppliction.Match(.Cells(1,ii),MyVals,0)
If Not IsError(x) Then...

Hope this helps..
 
Upvote 0
Try using a Select Case Statement

Code:
Select Case .Cells(1,ii)
Case "Comments","ABC",...,"LSE"
'do something
end select
 
Upvote 0
Another way:

Code:
Select Case .Cells(1, ii).Value
    Case "Comments", "ABC", "CDE", "LEE", "LSE"
'        Do stuff
End Select
 
Upvote 0
Code:
Code Select Case .cells(1,ii)
Case "Comments","ABC","CDE","LEE","LSE"
 'some code
Case Else:
End Select

lenze

Edit: Darn I'm slow
 
Upvote 0
Ah, okay, thanks a lot. I figured there was probably an array related solution. And the "case" solution I guess is sort of like a switch in Java, which was what I was looking for (I just started learning VBA, and Java is the only other programming language I know; I can't help but constantly connect the two).
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,730
Members
448,987
Latest member
marion_davis

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