'Like' in VBA

Nomis_Eswod

Board Regular
Joined
Jul 27, 2005
Messages
153
Hello,

I am doing some work in VBA and I have one last bit of coding to do that I can't work out.

Basically, on my Excel sheets there are bits of information called 'Keys'. The key codes are:
10
12
50
51
94
96
99

There are 2 places on the spreadsheet that use these keys, lets says Key1 is the first section and Key2 is the second. I have an If formula in the VBA to say that if Key1 = Key 2 then do the following. I need to amend this slightly though. What I need it to say is that If they are part of the same key group then do the following. Grouping is simple, there are 3 groups, 10 & 12, 50 & 51 and 94 & 96 & 99.

So for example,

Code:
if Key1 = 10 or 12 and Key2 = 10 or 12 Then
Do the following
End If

But I don't want to have to write out every possible combination.

Any ideas??
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.

al_b_cnu

Well-known Member
Joined
Jul 18, 2003
Messages
4,494
Hi,

How about something like:
Code:
If Key1 <= 12 And Key2 <= 12 Then
    MsgBox "Group1"
ElseIf Key1 <= 51 And Key2 <= 51 Then
    MsgBox "Group 2"
ElseIf Key1 <= 96 And Key2 <= 96 Then
    MsgBox "Group 3"
Else
    MsgBox "Group 4"
End If
 

Nomis_Eswod

Board Regular
Joined
Jul 27, 2005
Messages
153
It doesn't matter what the key is, they will always do the same thing, it's just that the 'Do This' bit will only happen if they are from a different Key Group.
 

al_b_cnu

Well-known Member
Joined
Jul 18, 2003
Messages
4,494
Hi,

You could introduce a 'Group' variable:
Code:
Select Case Key1
Case 10, 12
    Group1 = 1
Case 50, 51
    Group1 = 2
Case 94, 96
    Group1 = 3
Case 99
    Group1 = 4
End Select

Select Case Key2
Case 10, 12
    Group2 = 1
Case 50, 51
    Group2 = 2
Case 94, 96
    Group2 = 3
Case 99
    Group2 = 4
End Select

and test Group1 against Group2
 

Watch MrExcel Video

Forum statistics

Threads
1,130,448
Messages
5,642,218
Members
417,262
Latest member
andrewd1

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