Select Case - With multiple variables?

Stildawn

Board Regular
Joined
Aug 26, 2012
Messages
188
Hi All

I have a bunch of data and I need a why to test if a set of conditions are met, and then do more code if different sets are.

For example, 3 variables which are in A1, B1 & C1.

I was wondering if there was a way to get a select case type code to work but each case is testing each value in the A1, B1, C1 above.

Completely made up code below which explains what I am after:

Code:
Select Case
  Case Is A1 = 1 and B1 = 2 and C1 = 3
    Do something here
  Case Is A1 = 2 and B1 = 1 and C1 = 4
    Do something else here
  Case Is A1 = 5 and B1 = 6 and C1 = 1
    Do something different here
End Select

I know how to do standard select cases and how to use ranges etc the above is just a made up example to get my point across.

Or is there another way to do the above? Basically test multiple variables combined to execute different sets of code, without of course doing massive If statements or select cases within select cases etc.

Any help would be greatly appreciated.

Thanks
 

Some videos you may like

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().

steve the fish

Well-known Member
Joined
Oct 20, 2009
Messages
8,110
Office Version
  1. 365
Platform
  1. Windows
Heres a possible:

Code:
Select Case Range("A1") & "|" & Range("B1") & "|" & Range("C1")
    Case "1|2|3"
        MsgBox "yes"
    'etc
End Select

Just be aware of its limitations if pipes may be part of the cell contents. Very unlikely to get wrong result but potential.
 

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
46,201
Office Version
  1. 365
Platform
  1. Windows
If A1, B1 and C1 are variables (similar could be done if they are cell references but the syntax would change a little) then you can use a Select Case structure like this

Code:
Select Case True
  Case A1 = 1 And B1 = 2 And C1 = 3
    Do something here
  Case A1 = 2 And B1 = 1 And C1 = 4
    Do something else here
  Case A1 = 5 And B1 = 6 And C1 = 1
    Do something different here
End Select
 

Watch MrExcel Video

Forum statistics

Threads
1,108,771
Messages
5,524,795
Members
409,600
Latest member
Dunnowhatfor

This Week's Hot Topics

Top