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

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.

Jonmo1

MrExcel MVP
Joined
Oct 12, 2006
Messages
44,061
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

Oaktree

MrExcel MVP
Joined
Jun 20, 2002
Messages
8,108
Office Version
  1. 365
Try using a Select Case Statement

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

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
Another way:

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

lenze

Legend
Joined
Feb 18, 2002
Messages
13,690
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
L

Legacy 96851

Guest
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,191,274
Messages
5,985,693
Members
439,974
Latest member
sjoerdbosch

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
Top