If/Then with Multiple Ifs

helpexcel

Well-known Member
Joined
Oct 21, 2009
Messages
656
Hi - I'm wondering if I can define a letter, "X", to represent multiple values in an If/Then statement. The code is not complete and just an example of what i would like to do:
Code:
x="Apples", "Oranges", "Pears", "Grapes", "Banana", "Strawberry", "Blueberry"

If Sheet1.Range("A1") = x Then

Rest of Code

End If
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
You could do something like
Code:
Sub helpexcel()
   Dim St As String
   St = "Apples|Oranges|Pears|Grapes"
   If InStr(1, St, "|" & Sheet1.Range("A1") & "|", vbTextCompare) > 0 Then
     'do something
   End If
End Sub
 
Upvote 0
Like this:
Code:
Dim x as variant, i as long
x=Array("Apples", "Oranges", "Pears", "Grapes", "Banana", "Strawberry", "Blueberry")
for i = Lbound(x) to Ubound(x)
     If Sheet1.Range("A1") = x(i) then
       'rest of code
 
Upvote 0
You could also use a Case statement. This is really helpful if you have other options that you want to handle differently.
Here is a simple example based on your description:
Code:
Sub Test()

    Select Case Sheet1.Range("A1")
        Case "Apples", "Oranges", "Pears", "Grapes", "Banana", "Strawberry", "Blueberry"
            MsgBox "Item found in list"
        Case Else
            MsgBox "Item NOT found in list"
    End Select
    
End Sub
 
Upvote 0
If the case sensitivity does not matter, then:

Code:
Sub test()
  Dim x As String
  x = "Apples, Oranges, Pears, Grapes, Banana, Strawberry, Blueberry[SIZE=3][COLOR=#ff0000][B],[/B][/COLOR][/SIZE]"
  If LCase(x) Like "*" & LCase(Sheet1.Range("A1")) & "[B],[/B]*" Then
    'Rest of Code
  End If
End Sub

Note: It is important to put the comma shown in red.


------------

@Fluff, Missing | at the beginning and end of the string.
Code:
St = "[B][COLOR=#ff0000]|[/COLOR][/B]Apples|Oranges|Pears|Grapes[B][COLOR=#ff0000]|[/COLOR][/B]"
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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