'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

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
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
 
Upvote 0
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.
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,422
Messages
6,119,395
Members
448,891
Latest member
tpierce

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