Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
dan dan is offline
external usenet poster
 
Posts: 866
Default IF statement to check against list of values

I'm looking to right an if statment in VBA that accomplishes this;

IF var1 IN ("XXX", "YYY", "ZZZ") THEN
DoThing1
ELSE
DoThing2
Endif

I tried it as above, and it didn't like the use of "IN"
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,173
Default IF statement to check against list of values

Dan

IN is a SQL command, try a select case statement

Sub SelectCase()
Select Case var1
Case Is = "XXX"
MsgBox "XXX"
Case Is = "YYY"
MsgBox "YYY"
Case Is = "ZZZ"
MsgBox "ZZZ"
Case Else
MsgBox "Something Else"
End Select
End Sub


--
HTH
Nick Hodge
Microsoft MVP - Excel
Southampton, England
www.nickhodge.co.uk


"Dan" wrote in message
...
I'm looking to right an if statment in VBA that accomplishes this;

IF var1 IN ("XXX", "YYY", "ZZZ") THEN
DoThing1
ELSE
DoThing2
Endif

I tried it as above, and it didn't like the use of "IN"



  #3   Report Post  
Posted to microsoft.public.excel.programming
dan dan is offline
external usenet poster
 
Posts: 866
Default IF statement to check against list of values

Nick, thanks for the response. I think in the example you give, you have
different message boxes coming up for the different options (XXx, YYY, ZZZ).
What I want is it to do the same thing if VAR1 equals either XXX, YYY or ZZZ,
and something else if VAR1 is anything but XXX, YYY, or ZZZ.

"Nick Hodge" wrote:

Dan

IN is a SQL command, try a select case statement

Sub SelectCase()
Select Case var1
Case Is = "XXX"
MsgBox "XXX"
Case Is = "YYY"
MsgBox "YYY"
Case Is = "ZZZ"
MsgBox "ZZZ"
Case Else
MsgBox "Something Else"
End Select
End Sub


--
HTH
Nick Hodge
Microsoft MVP - Excel
Southampton, England
www.nickhodge.co.uk

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default IF statement to check against list of values


1. You could use a delimited list and then

if instr(1, "!A!B!C!" , "!" & myvar & "!") < 0 then etc

2. use

Select Case myvar
Case "A", "B", "C"
'do my stuff
End Select


3 create your own function to do it such as

Function myIN(strSearch, ParamArray strIN()) As Boolean
Dim v As Variant

myIN = False

For Each v In strIN()
If strSearch = v Then
myIN = True
Exit For
End If
Next
End Function

if myin(myvar,"B","C","A") then

Regards


--
tony h
------------------------------------------------------------------------
tony h's Profile: http://www.excelforum.com/member.php...o&userid=21074
View this thread: http://www.excelforum.com/showthread...hreadid=507445

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Formula to check against a list of valid values Barb W[_2_] Excel Worksheet Functions 3 March 4th 08 06:51 PM
How do I check for values in a list in an if statement? DJC Excel Discussion (Misc queries) 1 July 27th 05 07:13 PM
Check a user-input value against a list of values? Bill_S Excel Programming 1 April 28th 05 03:24 AM
Check for list of 3 values (alpha) in a column GeeSexAah Excel Programming 2 November 28th 04 11:37 PM
Check for list of 3 values (alpha) in a column GeeSexAah[_2_] Excel Programming 0 November 22nd 04 12:08 PM


All times are GMT +1. The time now is 05:37 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"