Results 1 to 8 of 8

VBA and IF/OR statement

This is a discussion on VBA and IF/OR statement within the Excel Questions forums, part of the Question Forums category; I know how to use an IF statement in VBA . Is there a way to include an OR in ...

  1. #1
    Board Regular
    Join Date
    Jul 2004
    Posts
    160

    Default VBA and IF/OR statement

    I know how to use an IF statement in VBA. Is there a way to include an OR in the IF statement? Any help or suggestions would be much appreciated.

    Thanks,
    Steve

  2. #2
    MrExcel MVP Oaktree's Avatar
    Join Date
    Jun 2002
    Location
    Chicago, IL
    Posts
    7,745

    Default

    As an example:

    sub test()
    If Range("A1") = 1 Or Range("A1") = 2 Then MsgBox "A1 equals either 1 or 2"
    end sub
    "The greatest challenge to any thinker is stating the problem in a way that will allow a solution." Bertrand Russell

  3. #3
    MrExcel MVP Joe Was's Avatar
    Join Date
    Feb 2002
    Location
    Central Florida, USA
    Posts
    7,539

    Default Re: VBA and IF/OR statement

    String full If Tests with:

    And
    Or

    Like: If (myVal is > 5 And myVal <> 0) Then
    JSW: Try and try again: "The way of the Coder!"

  4. #4
    Legend NateO's Avatar
    Join Date
    Feb 2002
    Location
    Minneapolis, Mn, USA
    Posts
    9,702

    Default Re: VBA and IF/OR statement

    Also, search the VBE help file for Using Select Case Statements.

  5. #5
    Board Regular
    Join Date
    Jul 2004
    Posts
    160

    Default Re: VBA and IF/OR statement

    Thanks for all of the helpful answers. I will go and try them now.

    Thanks again,
    Steve

  6. #6
    MrExcel MVP Anne Troy's Avatar
    Join Date
    Feb 2002
    Location
    Allentown, PA
    Posts
    2,547

    Default Re: VBA and IF/OR statement

    Thank you, Oaktree.

    RIP, Nate.
    ~Anne Troy

  7. #7
    New Member
    Join Date
    Nov 2008
    Posts
    45

    Default Re: VBA and IF/OR statement

    Thanks Joe Was
    Your suggested
    If (myVal is > 5 And myVal <> 0) Then
    led me to
    If (myVal is > 5 And myVal <> 0) Or (myVal is > 15 And myVal <> 10) Then
    which was a great help

  8. #8
    New Member grotten's Avatar
    Join Date
    Jun 2012
    Location
    Germany
    Posts
    1

    Default Re: VBA and IF/OR statement

    Quote Originally Posted by mdocton View Post
    Thanks Joe Was
    Your suggested
    If (myVal is > 5 And myVal <> 0) Then
    led me to
    If (myVal is > 5 And myVal <> 0) Or (myVal is > 15 And myVal <> 10) Then
    which was a great help

    I was looking for "If with Or" statements in VBA and came across this thread. Two questions rose in my mind: does "myVal is > 5" work with your Excel? I have to leave out the "is"... and the other question: what is the reason for this combination of arguments? I can get the same result with the statement

    If myVal > 5 Then

    Nonetheless it helped me for the syntax. Thanks!

    Gordon
    Last edited by grotten; Jun 15th, 2012 at 03:49 AM.

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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


DMCA.com