Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Correct syntax for IF statement???

Status
Not open for further replies.

Apollo6

Technical User
Jan 27, 2000
418
US
I cannot seem to remember the syntax for the following If statement:

If strCode in ("400A","401A","402A","403A","404A") Then
--Do Something--
End If

What I want is that if strCode equals any of the values then do something.

Thanks in advance for the refresher.
 
I can't seem to figure out how to make that line work, but I have a work around for you:

Select Case "402A"
Case "400A", "401A", "402A", "403A", "404A"
' --- Do Something ---

Case Else
' --- Do Something Else ---

End Select


Hope this helps!

Glenn
 
Thanks for suggestion Glenn. I thought about doing the case select scenario as well and decided on the scenario below. It just seems like that there is an easier way to compare against multiple possibilities.

If strCode Like "400A" Or strCode Like "401A" Or strCode Like "402A" Or strCode Like "403A" Or strCode Like "404A" Then
--Do Something--
Else
--Do Something Else--
End If
 
How's this?

Select Case strcode
Case "400A", "401A", "402A", "403A", "404A"
'--Do Something--
Case Else
'--Do Something Else--
End Select
 
Or this (which comes close to your original syntax):
Code:
Option Explicit

Sub test()
  If LookIn("401A", Array("400A", "401A", "402A", "403A", "404A")) Then
    MsgBox "Found"
  Else
    MsgBox "not in array"
  End If
End Sub

Function LookIn(What As String, Where As Variant) As Boolean
  If WorksheetFunction.Lookup(What, Where) = What Then LookIn = True
End Function
Somewhat limited in that the array elements must be in ascending sequence.
 
The last two suggestions would work fine.

I have Group "A" containing 43 codes and Group "B" containing 15 codes. I then have to compare my current variable(search code) against both groups.

Just seems like a lot of extra typing but I've now done the big If Then Else statement and it works, just not pretty.

Thanks for the ideas!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top