[Excel VBA] If with multiple conditions
Hmm, I don't think I've asked this before. I want to do an if or select statement where I'm checking if a state is one of maybe ten different states. I'm no expert programmer, but I know in Python, you can do "in", so I'm wondering if there's a way to do something like that. For example
Code:
If state in {"Arkansas", "California", "Maine", "New Jersey", "Wyoming"} then do something
as opposed to the much harder to write and read, especially if you have 10 or 15 states
Code:
If (state = "Arkansas" or state = "California" or state = "Maine" or state = "New Jersey" or state = "Wyoming") then do something
Thanks
Re: [Excel VBA] If with multiple conditions
use select case
Code:
Select Case State
Case "Arkansas", "California" '..... u can add more
'do something
End Select
Re: [Excel VBA] If with multiple conditions
Okay, thanks. I have used that before but I forgot. I think my problem is I want to work with two variables in reality, state and a range of dates. In that case, all I know about is doing Select Case True, or something like that. Then my cases involve having Date <= #3/3/2011# and Date >= #3/1/2011# and State one of 5 or 10 or whatever. Is there any way in that case?
Thanks for your help
Re: [Excel VBA] If with multiple conditions
this may help u
Code:
Dim State As String, Dt As Date, MyCheck1 As Boolean, MyCheck2 As Boolean
State = "Some2"
Dt = Date 'today
MyCheck1 = Dt <= #9/3/2011# And Dt >= #9/1/2011#
MyCheck2 = State = "Some1" Or State = "Some2" Or State = "Some3"
Select Case MyCheck1 And MyCheck2
Case True And True
'do something
End Select