Results 1 to 4 of 4

Thread: [Excel VBA] If with multiple conditions

  1. #1

    Thread Starter
    Member
    Join Date
    May 2011
    Posts
    42

    [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

  2. #2
    Just a Member! seenu_1st's Avatar
    Join Date
    Aug 2007
    Location
    India
    Posts
    2,170

    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
    Seenu

    If this post is useful, pls don't forget to Rate this post.
    Pls mark thread as resolved once ur problem solved.
    ADO Tutorial Variable types SP6 for VB6, MsFlexGrid fast fill, Sorting Algorithms


  3. #3

    Thread Starter
    Member
    Join Date
    May 2011
    Posts
    42

    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

  4. #4
    Just a Member! seenu_1st's Avatar
    Join Date
    Aug 2007
    Location
    India
    Posts
    2,170

    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
    Seenu

    If this post is useful, pls don't forget to Rate this post.
    Pls mark thread as resolved once ur problem solved.
    ADO Tutorial Variable types SP6 for VB6, MsFlexGrid fast fill, Sorting Algorithms


Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width